Help us understand the problem. What is going on with this article?

Array、List、Dictionaryの速度比較

More than 5 years have passed since last update.

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

duo
総合 ITベンチャーです。バーチャルライブ配信アプリ「IRIAM」、「ミライアカリ」をはじめとするバーチャルタレント事業、アミューズメント事業を展開しています。
https://zizai.co.jp/
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした