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

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - 大约在冬季

如何在IronPython中使用C#扩展方法 LINQ入门教程示例使用F#的实现 宝宝照片更新喽 如何让DevExpress.TreeList单元格中的自定义控件包含标签 见证中国A股市场:上午大盘加速寻底 沪指跌129点,探低至4241.02 漂亮宝宝100天啦!庆祝一下! F# 学习笔记(1/n) 权重股杀跌沪指半日破两关跌135点 IronPyton分析表达式 WCF服务契约 听课笔记 最近性能优化一些感触,分享中…… 两种不同字符串比较方法的性能对比 .Net 事件类型的实现和推荐做法 设计时支持:如何获取环境数据 如何在C#中调用 IronPython 代码 (基于IronPython 2.0A3) 运行Oracle数据库配置向导创建数据库失败ORA-24324的解决方案 C#3.0 自动属性——只能在简单属性上偷懒 性能——换个角度看问题 设计模式的滋味
再谈两种不同字符串比较方法的性能对比 - 大约在冬季 - 博客园
大约在冬季 · 2008-02-26 · via 博客园 - 大约在冬季

     以前曾经动笔写过一片《字符串比较方法的性能对比》的文章,很多朋友提出了非常好的意见和建议。碰巧最近看到好多人在这样比较字符串

if (string.Compare(keyState, "M"true, CultureInfo.InvariantCulture) == 0) {

似乎很有意思,大家都喜欢另辟蹊径的使用字符串比较,为了较为客观的反应各种字符串比较的优势,我特地做了一个本地测试,分别区分字符串长度一致以及长度不一致的情况,另外每种情况下还对字符串的比较方式上使用4种不同方法:
1、使用地球人都知道的“==”比较运算符
2、使用String.Compare方法,即上面提到的代码
3、使用string.Equals(string,StringComparison.Ordinal)方法
4、使用string.Equals(a, b, StringComparison.OrdinalIgnoreCase)方法
当然第三种方法是在不区分大小写和语言区域的情况下,每次进行1K万次比较。首先来看第一种场景,即字符串长度一致,但是内容不一致的情况。
使用比较的字符串用例为:
 string a = "abcdefghigklmnopqrstuvwxyz0123457689";
 string b = "abcdefghigklmnopqrstuv0123wxyz457689";

1、“==” :0.3239秒
2、使用String.Compare方法:1.8457秒
3、使用string.Equals(a, b, StringComparison.OrdinalIgnoreCase)方法 :1.6891秒
4、使用a.Equals(b, StringComparison.OrdinalIgnoreCase)方法 :1.6764秒

使用比较的字符串用例为:

 string a = "abcdefghigklmnopqrstuvwxyz0123457689字符串比较方法";
 string b = "abcdefghigklmnopqrstuvwxyz0123457689字符比较方法";

1、“==” :0.1766秒
2、使用String.Compare方法:.3.1811秒
3、使用string.Equals(a, b, StringComparison.OrdinalIgnoreCase)方法 :0.1093秒
4、使用a.Equals(b, StringComparison.OrdinalIgnoreCase)方法 :0.1112秒
比较的示例代码如下:

1 Stopwatch watch = new Stopwatch();
2             watch.Start();
3             for (int i = 0; i < 10000000; i++) {
4                 int r = String.Compare(a, b, true, CultureInfo.InvariantCulture);
5             }
6             watch.Stop();
7             double time;
8             time = watch.Elapsed.TotalSeconds;
9             Console.WriteLine("String.Compare  costs  {0}", time);

从比较的结果来看,使用最常用的“==”判断还是很有优势的,至少在各方面可以找到一个平衡点。在第二个场景中之所以第3、4测试方法比直接用“==”判断快的原因是,测试方法3,4首先检查了字符串的长度,如下代码:

 1 case StringComparison.OrdinalIgnoreCase:
 2             if (a.Length != b.Length)
 3             {
 4                 return false;
 5             }
 6             if (a.IsAscii() && b.IsAscii())
 7             {
 8                 return (nativeCompareOrdinal(a, b, true== 0);
 9             }
10             return (TextInfo.CompareOrdinalIgnoreCase(a, b) == 0);
11 

虽然有的书籍中介绍第4中方法会很快,但是至少从实际测试上来看却不是这样。此外,在进行WCF的WebService1.0的测试中并没有向微软宣称的那样比不是用WCF速度和性能上有明显的提升,只不过是旗鼓相当。不知道后续会有什么进展。