.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.希望大家一起交流!
谢谢!~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值