7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Listに初期サイズを指定した場合としてない場合で処理時間(パフォーマンス)を比べてみた

Last updated at Posted at 2017-11-27

.NETのListに初期サイズを指定した場合としてない場合で処理時間を比べてみました。

.NETのListは、以下のコンストラクタがあります。

List.cs
        //
        // 概要:
        //     空で、既定の初期量を備えた、System.Collections.Generic.List`1 クラスの新しいインスタンスを初期化します。
        public List();
        //
        // 概要:
        //     空で、指定した初期量を備えた、System.Collections.Generic.List`1 クラスの新しいインスタンスを初期化します。
        //
        // パラメーター:
        //   capacity:
        //     新しいリストに格納できる要素の数。
        //
        // 例外:
        //   T:System.ArgumentOutOfRangeException:
        //     capacity が 0 未満です。
        public List(int capacity);
        //
        // 概要:
        //     指定したコレクションからコピーした要素を格納し、コピーされる要素の数を格納できるだけの容量を備えた、System.Collections.Generic.List`1
        //     クラスの新しいインスタンスを初期化します。
        //
        // パラメーター:
        //   collection:
        //     新しいリストに要素がコピーされたコレクション。
        //
        // 例外:
        //   T:System.ArgumentNullException:
        //     collection は null です。
        public List(IEnumerable<T> collection);

今回はList()と、List(int capacity)でたくさんの要素を与えて処理時間を計測してみます。

コードはこんな感じ

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            

            FillListWithoutInitArray("No Init, 100 ",100);
            FillListWithInitArray("Init, 100", 100);

            FillListWithoutInitArray("No Init, 1000 ", 1000);
            FillListWithInitArray("Init, 1000", 1000);

            FillListWithoutInitArray("No Init, 10000 ", 10000);
            FillListWithInitArray("Init, 10000", 10000);

            FillListWithoutInitArray("No Init, 50000 ", 50000);
            FillListWithInitArray("Init, 50000", 50000);


            FillListWithoutInitArray("No Init, 100000 ", 100000);
            FillListWithInitArray("Init, 100000", 100000);

            FillListWithoutInitArray("No Init, 1000000 ", 1000000);
            FillListWithInitArray("Init, 1000000", 1000000);

            FillListWithoutInitArray("No Init, 10000000 ", 10000000);
            FillListWithInitArray("Init, 10000000", 10000000);

            FillListWithoutInitArray("No Init, 50000000 ", 50000000);
            FillListWithInitArray("Init, 50000000", 50000000);

            Console.ReadKey();

        }

        private static void FillListWithoutInitArray(string title, int size)
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();


            var datalist = new List<Data>();
            for (int i = 0; i < size; i++)
            {
                datalist.Add(new Data());
            }

            sw.Stop();
            Console.WriteLine(string.Format("{0, -20}", title) + ":" + sw.Elapsed);
        }

        private static void FillListWithInitArray(string title, int size)
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();


            var datalist = new List<Data>(size);
            for (int i = 0; i < size; i++)
            {
                datalist.Add(new Data());
            }

            sw.Stop();
            Console.WriteLine(string.Format("{0, -20}", title) + ":" + sw.Elapsed);
        }

        class Data
        {
            public Data()
            {
                data10 = "00000000000000000000000000000000000000000000000000000000000000000000000";
            }

            public int data1 { get; set; }
            public int data2 { get; set; }
            public int data3 { get; set; }
            public int data4 { get; set; }
            public int data5 { get; set; }
            public int data6 { get; set; }
            public int data7 { get; set; }
            public int data8 { get; set; }
            public int data9 { get; set; }
            public string data10 { get; set; }
        }
    }
}


結果

1回目
キャプチャ1.PNG

2回目
キャプチャ2.PNG

考察

環境にも依存するが、
50000要素ぐらいまではcapacity指定したほうが速い。
100000要素からはcapacity指定しないほうが速い。
要素が非常に多いほどcapacity指定しないほうが良いかも知れない。

Any CPUでビルドした、64bit windowsマシンで実行しています。メモリは16GB

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?