.NET深入学习笔记(2):C#中判断空字符串的4种方法性能比较与分析

简介:
偶然看到<C#中判断空字符串的3种方法性能分析 >作者:清清月儿  主页:http://blog.csdn.net/21aspnet/           时间:2007.4.28
写的一篇关于字符串为空判断方法的性能分析文章,实验结果作者已经给出,结论是使用.length==0判断的效率最高,但是文章的结尾只有一句话,感觉不够详细,所以自己写下这个文章,算一个补充和学习吧。
【1】 程序代码执行的硬件环境:
CPU
Intel T2300 1.66GHz
内存
Kingston  DDR2 667 1G
硬盘
80G  5400  8m
     测试的软件环境:
OS
Windows XP Pro
IDE
VS 2008 RTM
【2】测试的代码如下:
定义了3个变量,分别调用4种方法,进行100w次判断,记录测试时间:
InBlock.gifStopwatch sw =  new Stopwatch(); //实例化一个对象,记录时间 
InBlock.gif                         string sEmpty1 =  string.Empty; //实例化3个字符串对象,赋值如下。分别作空比较试验 
InBlock.gif                         string sEmpty2 = ""; 
InBlock.gif                         string sEmpty3 =  "StringNotEmpty"
InBlock.gif                         ////////////////////////////////////////////////////Test sEmpty1/////////////////////////// 
InBlock.gif                        sw.Start(); //开始记录 
InBlock.gif                         for ( int i = 0; i <= 1000000; i++) 
InBlock.gif                        { 
InBlock.gif                                 if (sEmpty1.Length == 0)    
InBlock.gif                                { } 
InBlock.gif                        } 
InBlock.gif                        sw.Stop(); //停止记录时间 
InBlock.gif                        Console.WriteLine( "string.Empty Length == 0 Time Cost is {0}", sw.ElapsedMilliseconds); 
InBlock.gif                         //////// 
InBlock.gif                        sw.Reset(); //重置计数器为0; 
InBlock.gif                        sw.Start(); 
InBlock.gif                         for ( int i = 0; i <= 1000000; i++) 
InBlock.gif                        { 
InBlock.gif                                 if (sEmpty1 == "") 
InBlock.gif                                { } 
InBlock.gif                        } 
InBlock.gif                        sw.Stop(); 
InBlock.gif 
InBlock.gif                        Console.WriteLine( "string.Empty == \"\" Time Cost is {0}", sw.ElapsedMilliseconds); 
InBlock.gif                         //// 
InBlock.gif                        sw.Reset(); 
InBlock.gif                        sw.Start(); 
InBlock.gif                         for ( int i = 0; i <= 1000000; i++) 
InBlock.gif                        { 
InBlock.gif                                 if (sEmpty1 ==  string.Empty)    
InBlock.gif                                { } 
InBlock.gif                        } 
InBlock.gif                        sw.Stop(); 
InBlock.gif                        Console.WriteLine( "string.Empty    == string.Empty Time Cost is {0}", sw.ElapsedMilliseconds); 
InBlock.gif 
InBlock.gif                        sw.Reset(); 
InBlock.gif                        sw.Start(); 
InBlock.gif                         for ( int i = 0; i <= 1000000; i++) 
InBlock.gif                        { 
InBlock.gif                                 if( string.IsNullOrEmpty(sEmpty1)) 
InBlock.gif                                {} 
InBlock.gif                        } 
InBlock.gif                        sw.Stop(); 
InBlock.gif                        Console.WriteLine( "string.IsNullOrEmpty Time Cost is {0}", sw.ElapsedMilliseconds); 
InBlock.gif                        Console.WriteLine(); 
InBlock.gif                         ////////////////////////////////////////////////////Test sEmpty2/////////////////////////// 
InBlock.gif                        sw.Reset(); 
InBlock.gif                        sw.Start(); 
InBlock.gif                         for ( int i = 0; i <= 1000000; i++) 
InBlock.gif                        { 
InBlock.gif                                 if(sEmpty2.Length == 0) 
InBlock.gif                                {} 
InBlock.gif                        } 
InBlock.gif                        sw.Stop(); 
InBlock.gif                        Console.WriteLine( "\"\" Length == 0 Time Cost is {0}", sw.ElapsedMilliseconds); 
InBlock.gif                         //////// 
InBlock.gif                        sw.Reset(); 
InBlock.gif                        sw.Start(); 
InBlock.gif                          for ( int i = 0; i <= 1000000; i++) 
InBlock.gif                        { 
InBlock.gif                                 if(sEmpty2 == "") 
InBlock.gif                                {} 
InBlock.gif                        } 
InBlock.gif                        sw.Stop(); 
InBlock.gif                        Console.WriteLine( "\"\" == \"\" Time Cost is {0}", sw.ElapsedMilliseconds); 
InBlock.gif                         ///// 
InBlock.gif                        sw.Reset(); 
InBlock.gif                        sw.Start(); 
InBlock.gif                         for ( int i = 0; i <= 1000000; i++) 
InBlock.gif                        { 
InBlock.gif                                 if(sEmpty2 ==  string.Empty) 
InBlock.gif                                {} 
InBlock.gif                        } 
InBlock.gif                        sw.Stop(); 
InBlock.gif                        Console.WriteLine( "\"\"    == string.Empty Test3 Time Cost is {0}", sw.ElapsedMilliseconds); 
InBlock.gif                         ///// 
InBlock.gif                        sw.Reset(); 
InBlock.gif                        sw.Start(); 
InBlock.gif                         for ( int i = 0; i <= 1000000; i++) 
InBlock.gif                        { 
InBlock.gif                                 if( string.IsNullOrEmpty(sEmpty2)) 
InBlock.gif                                {} 
InBlock.gif                        } 
InBlock.gif                        sw.Stop(); 
InBlock.gif                        Console.WriteLine( "\"\" string.IsNullOrEmpty Time Cost is {0}", sw.ElapsedMilliseconds); 
InBlock.gif                        Console.WriteLine(); 
InBlock.gif                         ////////////////////////////////////////////////////Test sEmpty3/////////////////////////// 
InBlock.gif                        sw.Reset(); 
InBlock.gif                        sw.Start(); 
InBlock.gif                         for ( int i = 0; i <= 1000000; i++) 
InBlock.gif                        { 
InBlock.gif                                 if(sEmpty3.Length == 0) 
InBlock.gif                                {} 
InBlock.gif                        } 
InBlock.gif                        sw.Stop(); 
InBlock.gif                        Console.WriteLine( "\"StringNotEmpty\" Length == 0 Time Cost is {0}", sw.ElapsedMilliseconds); 
InBlock.gif                         //////// 
InBlock.gif                        sw.Reset(); 
InBlock.gif                        sw.Start(); 
InBlock.gif                         for ( int i = 0; i <= 1000000; i++) 
InBlock.gif                        { 
InBlock.gif                                 if(sEmpty3 == "") 
InBlock.gif                                {} 
InBlock.gif                        } 
InBlock.gif                        sw.Stop(); 
InBlock.gif                        Console.WriteLine( "\"StringNotEmpty\" == \"\" Time Cost is {0}", sw.ElapsedMilliseconds); 
InBlock.gif                         //// 
InBlock.gif                        sw.Reset(); 
InBlock.gif                        sw.Start(); 
InBlock.gif                         for ( int i = 0; i <= 1000000; i++) 
InBlock.gif                        { 
InBlock.gif                                 if(sEmpty3 ==  string.Empty) 
InBlock.gif                                {} 
InBlock.gif                        } 
InBlock.gif                        sw.Stop(); 
InBlock.gif                        Console.WriteLine( "\"StringNotEmpty\"    == string.Empty Test3 Time Cost is {0}", sw.ElapsedMilliseconds); 
InBlock.gif                         //// 
InBlock.gif                        sw.Reset(); 
InBlock.gif                        sw.Start(); 
InBlock.gif                         for ( int i = 0; i <= 1000000; i++) 
InBlock.gif                        { 
InBlock.gif                                 if( string.IsNullOrEmpty(sEmpty3)) 
InBlock.gif                                {} 
InBlock.gif                        } 
InBlock.gif                        sw.Stop(); 
InBlock.gif                        Console.WriteLine( "\"StringNotEmpty\" IsNullOrEmpty Time Cost is {0}", sw.ElapsedMilliseconds);
代码的运行结果如下:
结果分析来看,调用string的length==0作比较,不论字符串是否为空,此方法的效率最高,此点与清清月儿的结果一致;
string的isNullOrEmpty()方法的效率基本不变,无论字符串是否有值;
== string.Empty== ""两种方法在3个变量测试的实验中效率相对较低,但是两者再和对方比较的时候会出现效率降低的情况,见上图;
【3】原因剖析:
  原因是什么呢?我们来看看对应的il代码:
InBlock.gif1.locals init ([0]  class [System]System.Diagnostics.Stopwatch sw, 
InBlock.gif 2                     [1]  string sEmpty1, 
InBlock.gif 3                     [2]  string sEmpty2, 
InBlock.gif 4                     [3]  string sEmpty3, 
InBlock.gif 5                     [4] int32 i, 
InBlock.gif 6                     [5]  bool CS$4$0000) 
InBlock.gif 7    IL_0000:    nop 
InBlock.gif 8    IL_0001:    newobj         instance  void [System]System.Diagnostics.Stopwatch::.ctor() 
InBlock.gif 9    IL_0006:    stloc.0 
InBlock.gif10    IL_0007:    ldsfld          string [mscorlib]System.String::Empty //将指定字段的值推送到堆栈上。 ldsfld 指令将静态(在类的所有实例中共享)字段的值推送到堆栈上。返回类型是与传递的元数据标记 field 关联的类型。 
InBlock.gif11 
InBlock.gif12    IL_000c:    stloc.1 
InBlock.gif13    IL_000d:    ldstr            "" //将对字符串的对象引用推送到堆栈上,ldstr 指令推送对表示在元数据中存储的特定字符串的新字符串对象的对象引用(O 类型)。 
InBlock.gif14    IL_0012:    stloc.2 
InBlock.gif15    IL_0013:    ldstr             "StringNotEmpty" //将对字符串的对象引用推送到堆栈上,ldstr 指令推送对表示在元数据中存储的特定字符串的新字符串对象的对象引用(O 类型)。 
InBlock.gif16    IL_0018:    stloc.3 
InBlock.gif17    IL_0019:    ldloc.0
两者的差别由于推送到堆栈上的内容不同,前者是静态共享值推送到堆栈,后者是字符串对象的地址推送到堆栈.
造成的比较差别.另外字符串值是否相等的资料大家可以看看园子里缘清的文章,有很好的参考价值.地址: http://www.cnblogs.com/isline/archive/2009/02/04/1383799.html.希望大家一起交流!
谢谢!~


 本文转自 frankxulei 51CTO博客,原文链接:http://blog.51cto.com/frankxulei/318543,如需转载请自行联系原作者



相关文章
|
1天前
|
API 网络安全 数据安全/隐私保护
.NET邮箱API发送邮件的方法有哪些
本文介绍了.NET开发中使用邮箱API发送邮件的方法,包括SmtpClient类发送邮件、MailMessage类创建邮件消息、设置SmtpClient属性、同步/异步发送、错误处理、发送HTML格式邮件、带附件邮件以及多人邮件。AokSend提供高触达发信服务,适用于大规模验证码发送场景。了解这些技巧有助于开发者实现高效、可靠的邮件功能。
|
1天前
|
安全 数据安全/隐私保护 开发者
三款.NET 代码混淆工具比较分析:ConfuserEx、Obfuscar 和 Ipa Guard
三款.NET 代码混淆工具比较分析:ConfuserEx、Obfuscar 和 Ipa Guard
|
1天前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
1天前
|
C#
C#学习相关系列之数组---常用方法使用(二)
C#学习相关系列之数组---常用方法使用(二)
|
1天前
|
开发框架 .NET C语言
LabVIEW中使用.NET方法时出现错误1316
LabVIEW中使用.NET方法时出现错误1316
14 4
|
1天前
|
程序员 C#
C#抽象类和抽象方法详解
C#抽象类和抽象方法详解
10 0
|
1天前
|
存储 开发框架 .NET
C#中将DataTable转化成ListT的方法解析
C#中将DataTable转化成ListT的方法解析
8 0
|
1天前
|
数据采集 前端开发 数据挖掘
Fizzler库+C#:从微博抓取热点的最简单方法
本文介绍如何使用Fizzler库和C#构建微博热点信息爬虫。通过Fizzler的CSS选择器定位关键信息,提取热点标题和排名,实现微博内容的智能挖掘。示例代码展示单线程和多线程采集方法,并涉及代理IP使用。
Fizzler库+C#:从微博抓取热点的最简单方法
|
1天前
|
存储 数据采集 API
C# GetField 方法应用实例
C# GetField 方法应用实例
|
1天前
|
JSON 安全 API
C# GetMethod 方法应用实例
C# GetMethod 方法应用实例