Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
25
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@taiga_takahari

Array、List、Dictionaryの速度比較

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が意外だった感。

25
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
25
Help us understand the problem. What is going on with this article?