惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
Cloudbric
Cloudbric
G
GRAHAM CLULEY
S
Securelist
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
Spread Privacy
Spread Privacy
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tailwind CSS Blog
博客园_首页
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
O
OpenAI News
J
Java Code Geeks
T
Tenable Blog
K
Kaspersky official blog
AWS News Blog
AWS News Blog
S
Security @ Cisco Blogs
The GitHub Blog
The GitHub Blog
T
Threatpost
月光博客
月光博客
H
Heimdal Security Blog
Security Latest
Security Latest
The Hacker News
The Hacker News
Y
Y Combinator Blog
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
C
CERT Recently Published Vulnerability Notes
D
Docker
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net
V
Visual Studio Blog
H
Help Net Security

博客园 - 寒山潜龙

人脸识别算法总结 神解释:UART、I2C、SPI、1-wire四大通信接口 解决github不显示图片的问题 树莓派SD卡容量扩展的方法 树莓派安装opencv调用cv2时提示 ModuleNotFoundError: NO module named 'cv2’的解决方法 在数莓派32位系统中安装opencv 4.5.5 树莓派官方32位和64位系统换源 linux系统压缩备份 Linux 解决 hosts 文件无法修改: ‘readonly‘ option is set (add ! to override) Paddle Lite——报错解决:基于Paddle Lite Demo运行run.sh编译报错无法运行 Debian系统安装opencv github 打不开一招搞定! - 寒山潜龙 树莓派4B安装 百度飞桨paddlelite 做视频检测 (一、环境安装) C#之虚函数 非常清晰全面的讲解 今天有个朋友问我抽象方法和接口的区别,为了解释清楚这个事情,我在网上看到一篇文章讲的非常好给大家分享一下,也感谢原作者的付出 SQL2005四个排名函数(row_number、rank、dense_rank和ntile)的比较 Redis 集群方案 Android SDK Manager国内更新代理 AndroidDevTools简介 The status code returned from the server was: 500
『性能』List 和 HashSet 查找性能比较 (任何数据量的检索 从此只用 HashSet )
寒山潜龙 · 2024-07-22 · via 博客园 - 寒山潜龙

结论:

总数 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 查找的 分水岭 在哪里。

测试代码:

复制代码

 1         static void Main(string[] args)
 2         {
 3             List<string> list = new List<string>();
 4             HashSet<string> hash = new HashSet<string>();
 5 
 6             //数据准备
 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("数据准备完成");
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> 中, 判断数据是否存在, 耗时: {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> 中, 判断数据是否存在, 耗时: {1}", hash.Count, (time3 - time2).TotalSeconds);
39 
40 
41             Console.ReadKey();
42         }

复制代码

运行截图:

Hashtable 性能:

.Net 2.0 没有 HashSet,但是有 Hashtable 和 Dictionary 

Hashtable 支持  Key查找 和 Value查找

复制代码

 1             //hashtable - key 的查找性能
 2             DateTime time4 = DateTime.Now;
 3             bool result2 = true;
 4             foreach (string str in list)
 5             {
 6                 bool v = hash2.ContainsKey(str); //hashtable - key 的查找性能
 7                 result2 = result2 && v;
 8             }
 9             DateTime time5 = DateTime.Now;
10             Console.WriteLine("从 {0} 的 Hashtable 中, 判断Key是否存在, 耗时: {1}", hash2.Count, (time5 - time4).TotalSeconds);
11 
12 
13             //hashtable - value 的查找性能
14             DateTime time6 = DateTime.Now;
15             bool result3 = true;
16             foreach (string str in list)
17             {
18                 bool v = hash2.ContainsValue(str); //hashtable - value 的查找性能
19                 result3 = result3 && v;
20             }
21             DateTime time7 = DateTime.Now;
22             Console.WriteLine("从 {0} 的 Hashtable 中, 判断Value是否存在, 耗时: {1}", hash2.Count, (time7 - time6).TotalSeconds);

复制代码

测试结果如下: