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

推荐订阅源

The Register - Security
The Register - Security
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
WordPress大学
WordPress大学
博客园 - 【当耐特】
月光博客
月光博客
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
S
SegmentFault 最新的问题
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
C
Check Point Blog
G
Google Developers Blog
有赞技术团队
有赞技术团队
I
InfoQ
T
The Blog of Author Tim Ferriss
F
Full Disclosure
Help Net Security
Help Net Security
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
T
Tenable Blog
Know Your Adversary
Know Your Adversary
N
Netflix TechBlog - Medium
H
Hacker News: Front Page
C
Cyber Attacks, Cyber Crime and Cyber Security
Project Zero
Project Zero
Latest news
Latest news
Recorded Future
Recorded Future
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
News | PayPal Newsroom
C
Cisco Blogs
T
Tor Project blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Secure Thoughts
A
Arctic Wolf

博客园 - lovebanyi

Memory stream is not expandable 博客园密文招聘的解密 微软Azure平台 cloud service动态申请证书并绑定证书碰到的坑 PInvoke在 2.0 3.0的时候正常 升级到4.0后出错。 不要进行过度设计,某一层存在真的有意义吗?是否可以更简单。 化繁为简 定义权限 Ajax加载子域跨站cookie丢失的问题. ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" can not work - lovebanyi 支付系统会碰到的漏洞问题。 值类型引用类型List Remove 方法Remove不掉哪一个项。 流程变换与抽象 Compiler Error Message: CS1513: } expected 如何用好SVN的Branch EntityFramework Any 生成的sql语句超长。 Quartz 的SB问题 GetNextValidTimeAfter 输出和输出 时区 不同步,好傻的方法? 新的一家公司Web开发选择的控件是Dev Express 太烂了,发现项目结构也是混乱的。 使用lambda表达式对属性进行验证。 数据库连接字符串配置的流程改进 从解放劳动力来看未来的科技进程
Exists 比Contains 慢非常多。
lovebanyi · 2014-09-06 · via 博客园 - lovebanyi
void Main()
{
	List<string> s = new List<string>(){};
	for(int i=0;i<10000;i++)
	{
		s.Add(i.ToString());
	}

	s.Sort();
	

	Stopwatch sw0 = new Stopwatch();
	sw0.Start();
	for(int i=0;i<10000;i++)
	{
		var k0 =  s.Contains(i.ToString());
	}
	sw0.Stop();
	sw0.Elapsed.Dump();
	
	Stopwatch sw1 = new Stopwatch();
	sw1.Start();
	for(int i=0;i<10000;i++)
	{
		var k1 = s.Exists(it=>it==i.ToString());
	}
	sw1.Stop();
	sw1.Elapsed.Dump();
	
		Stopwatch sw2 = new Stopwatch();
	sw2.Start();
	for(int i=0;i<10000;i++)
	{
		var k2 = BinarySearch(s,i.ToString());
	}
	sw2.Stop();
	sw2.Elapsed.Dump();

}

public static bool BinarySearch(List<string> list, string value)
		
		{
			int min = 0;
			int max = list.Count;
			while (min < max)
			{
				int mid = (max + min) / 2;
				string midItem = list[mid];
			
				int comp = midItem.CompareTo(value);
				if (comp < 0)
				{
					min = mid + 1;
				}
				else if (comp > 0)
				{
					max = mid - 1;
				}
				else
				{
					return true;//midItem;
				}
			} 
			if (min == max && min!=list.Count&&
				list[min].CompareTo(value) == 0)
			{
				return true;
			}
		
			return false;
		}

 三个的执行时间。 拆半查找要求得要求先排序。

00:00:00.7093621

00:00:06.7513154

00:00:00.0159961