小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

『性能』List 和 HashSet 查找性能比較

 印度阿三17 2019-03-30

結(jié)論:

總數(shù) 50000 (5萬(wàn)): List 檢索 5W次 耗時(shí) 23秒, HashSet 檢索 5W次 耗時(shí) 0.01秒。

總數(shù) 5000   (5千): List 檢索 5K次 耗時(shí) 0.16秒, HashSet 檢索 5K次 耗時(shí) 0.001秒。

總數(shù) 500     (5百): List 檢索 500次 耗時(shí) 0.004秒, HashSet 檢索 500次 耗時(shí) 0.000秒。

總數(shù) 50                    : List 檢索 50次  耗時(shí) 0.002秒, HashSet 檢索 500次 耗時(shí) 0.000秒。

集合查找元素,

當(dāng)總數(shù)超過(guò) 10 時(shí),       HashSet<T>  的檢索性能 就會(huì)比 List<T> 快。

當(dāng)總數(shù)超過(guò) 1000 時(shí),   List<T> 的檢索性能 會(huì) 急速下降。

當(dāng)總數(shù)超過(guò) 10000 時(shí), List<T> 將會(huì)以 秒 為單位 的損失性能。

換言之:無(wú)論怎樣的數(shù)據(jù)量, HashSet<T> 的檢索速度 都要比 List<T> 快。(不存在那種: 多少數(shù)據(jù)量以下,List 有優(yōu)勢(shì),多少數(shù)據(jù)量以上,HashSet 有優(yōu)勢(shì)

背景:

今天在項(xiàng)目中,需要用到一個(gè) 檢索功能,只需要判斷 指定的關(guān)鍵字 是否存在。

第一本能就想到了 HashSet<T> 對(duì)象。

但,HashSet<T> 是 .Net 4.0 的東西,我希望自己的代碼 無(wú)限兼容 .Net 2.0 —— 所以想避開(kāi)這個(gè)東西。

其實(shí),我的關(guān)鍵字 最多不過(guò) 20個(gè),但是檢索次數(shù)比較多 —— 所以,我就想看一下 List 和 HashSet 查找的 分水嶺 在哪里。

測(cè)試代碼:

 1         static void Main(string[] args)
 2         {
 3             List<string> list = new List<string>();
 4             HashSet<string> hash = new HashSet<string>();
 5 
 6             //數(shù)據(jù)準(zhǔn)備
 7             for (int i = 0; i < 5000; i  )
 8             {
 9                 string str = Guid.NewGuid().ToString();
10                 list.Add(str);
11                 hash.Add(str);
12             }
13             Console.WriteLine("數(shù)據(jù)準(zhǔn)備完成");
14 
15 
16             //list 的查找性能
17             DateTime time0 = DateTime.Now;
18             bool result0 = true;
19             foreach (string str in list)
20             {
21                 bool v = list.Contains(str); //list 的查找性能
22                 result0 = result0 && v;
23             }
24             DateTime time1 = DateTime.Now;
25             Console.WriteLine("從 {0} 的 List<string> 中, 判斷數(shù)據(jù)是否存在, 耗時(shí): {1}", list.Count, (time1 - time0).TotalSeconds);
26 
27 
28 
29             //hash 的查找性能
30             DateTime time2 = DateTime.Now;
31             bool result1 = true;
32             foreach (string str in list)
33             {
34                 bool v = hash.Contains(str); //hash 的查找性能
35                 result1 = result1 && v;
36             }
37             DateTime time3 = DateTime.Now;
38             Console.WriteLine("從 {0} 的 HashSet<string> 中, 判斷數(shù)據(jù)是否存在, 耗時(shí): {1}", hash.Count, (time3 - time2).TotalSeconds);
39 
40 
41             Console.ReadKey();
42         }

運(yùn)行截圖:

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多