『性能』List 和 HashSet 查找性能比较 (任何数据量的检索 从此只用 HashSet )
结论:
总数 50000 (5万): List 检索 5W次 耗时 23秒, HashSet 检索 5W次 耗时 0.01秒。
总数 5000 (5千): List 检索 5K次 耗时 0.16秒, HashSet 检索 5K次 耗时 0.001秒。
总数 500 (5百): List 检索 500次 耗时 0.004秒, HashSet 检索 500次 耗时 0.000秒。
总数 50 : List 检索 50次 耗时 0.002秒, HashSet 检索 500次 耗时 0.000秒。
集合查找元素,
当总数超过 10 时, HashSet<T> 的检索性能 就会比 List<T> 快。
当总数超过 1000 时, List<T> 的检索性能 会 急速下降。
当总数超过 10000 时, List<T> 将会以 秒 为单位 的损失性能。
换言之:
> 无论怎样的数据量, HashSet<T> 的检索速度 都要比 List<T> 快。(不存在那种: 多少数据量以下,List 有优势,多少数据量以上,HashSet 有优势)
> Hastable 的查找性能 == HashSet 的查找性能,用不了 HashSet 可以用 Hashtable 替换。
背景:
今天在项目中,需要用到一个 检索功能,只需要判断 指定的关键字 是否存在。
第一本能就想到了 HashSet<T> 对象。
但,HashSet<T> 是 .Net 4.0 的东西,我希望自己的代码 无限兼容 .Net 2.0 —— 所以想避开这个东西。
其实,我的关键字 最多不过 20个,但是检索次数比较多 —— 所以,我就想看一下 List 和 HashSet 查找的 分水岭 在哪里。
测试代码:
static void Main(string[] args) { List<string> list = new List<string>(); HashSet<string> hash = new HashSet<string>(); //数据准备 for (int i = 0; i < 5000; i++) { string str = Guid.NewGuid().ToString(); list.Add(str); hash.Add(str); } Console.WriteLine("数据准备完成"); //list 的查找性能 DateTime time0 = DateTime.Now; bool result0 = true; foreach (string str in list) { bool v = list.Contains(str); //list 的查找性能 result0 = result0 && v; } DateTime time1 = DateTime.Now; Console.WriteLine("从 {0} 的 List<string> 中, 判断数据是否存在, 耗时: {1}", list.Count, (time1 - time0).TotalSeconds); //hash 的查找性能 DateTime time2 = DateTime.Now; bool result1 = true; foreach (string str in list) { bool v = hash.Contains(str); //hash 的查找性能 result1 = result1 && v; } DateTime time3 = DateTime.Now; Console.WriteLine("从 {0} 的 HashSet<string> 中, 判断数据是否存在, 耗时: {1}", hash.Count, (time3 - time2).TotalSeconds); Console.ReadKey(); }
运行截图:
Hashtable 性能:
.Net 2.0 没有 HashSet,但是有 Hashtable 和 Dictionary
Hashtable 支持 Key查找 和 Value查找
//hashtable - key 的查找性能 DateTime time4 = DateTime.Now; bool result2 = true; foreach (string str in list) { bool v = hash2.ContainsKey(str); //hashtable - key 的查找性能 result2 = result2 && v; } DateTime time5 = DateTime.Now; Console.WriteLine("从 {0} 的 Hashtable 中, 判断Key是否存在, 耗时: {1}", hash2.Count, (time5 - time4).TotalSeconds); //hashtable - value 的查找性能 DateTime time6 = DateTime.Now; bool result3 = true; foreach (string str in list) { bool v = hash2.ContainsValue(str); //hashtable - value 的查找性能 result3 = result3 && v; } DateTime time7 = DateTime.Now; Console.WriteLine("从 {0} 的 Hashtable 中, 判断Value是否存在, 耗时: {1}", hash2.Count, (time7 - time6).TotalSeconds);
测试结果如下:
- ObservableCollection与List在加载数据上的性能比较
- Map与List查找性能比较
- ArrayList、LinkedList、Vector性能比较
- 顺序容器vector,dequeue和list性能比较
- java list三种遍历方法性能比较
- STL容器 vector,list,deque 性能比较
- XML数据读取方式性能比较(一)
- 判断一个整数是否是n^m次方类型数据,并比较乘法和除法性能差异
- C# List<>与Array性能比较
- ArcEngine数据删除几种方法和性能比较[转]
- 常用数据结构及复杂度 array、LinkedList、List、Stack、Queue、Dictionary、SortedDictionary、HashSet、SortedSet
- python内置数据类型:list,tuple,dict比较
- YDB针对范围查找所做的性能小改进(skiplist IO 分析)
- XML数据读取方式性能比较(一)
- HBase 高性能获取数据(多线程批量式解决办法) + MySQL和HBase性能测试比较
- 关于如何从list中查找需要的匹配的数据
- Python数据类型:双端队列deque-比列表list性能更高的一种数据类型
- DataRabbit 轻量的数据访问框架(14)-- DataRabbit 3.0 与 Linq to sql 性能比较
- 常见数据结构查找、插入、删除、遍历性能比较 常见排序算法的比较(图)
- 在数据库中查找“第二”的各种方法以及性能比较