LoginSignup
28
28

More than 5 years have passed since last update.

Array、List、Dictionaryの速度比較

Last updated at Posted at 2014-04-02

ORM的な何かで取得したIEnumerable<T>のデータがあったとき、Array、List、Dictionaryへの変換速度と1件取得の速度を比較してみた。

IEnumerable<T> datas;
// 何かデータ取得処理
・・・

var sw = new Stopwatch();
sw.Start();
var array = datas.ToArray();
sw.Stop();
Console.WriteLine("Array変換:{0}", sw.Elapsed);

sw.Reset();
sw.Start();
var list = datas.ToList();
sw.Stop();
Console.WriteLine("List変換:{0}", sw.Elapsed);

sw.Reset();
sw.Start();
var dictionary = datas.ToDictionary(x => x.Id);
sw.Stop();
Console.WriteLine("Dictionary変換:{0}", sw.Elapsed);

// 検索キー取得
var Id = datas.OrderBy(x => Guid.NewGuid()).First().Id;

Console.WriteLine("Id:{0}", Id);

sw.Reset();
sw.Start();
var data1 = array.First(x => x.Id == Id);
sw.Stop();
Console.WriteLine("Array検索:{0}", sw.Elapsed);

sw.Reset();
sw.Start();
var data2 = list.First(x => x.Id == Id);
sw.Stop();
Console.WriteLine("List検索:{0}", sw.Elapsed);

sw.Reset();
sw.Start();
var data3 = dictionary[Id];
sw.Stop();
Console.WriteLine("Dictionary検索:{0}", sw.Elapsed);

①変換速度

回数 Array List Dictionary
1回目 0.0017449 0.0016148 0.0053250
2回目 0.0017138 0.0015178 0.0054479
3回目 0.0017235 0.0016059 0.0053376

②検索速度

回数 Array List Dictionary
1回目 0.0007101 0.0006819 0.0000109
2回目 0.0003431 0.0002906 0.0000105
3回目 0.0009737 0.0009600 0.0000109

変換速度はList < Array <<<<< Dictionayだったが、
検索速度はDictionary <<<<<<<<<< List < Arrayだった。

Dictionaryの結果は予想通りだったけど、ArrayとListが意外だった感。

28
28
1

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
28
28