永利最大(官方)网站

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

原创|其它|编辑:郝浩|2009-05-14 10:29:10.000|阅读 725 次

概述:写的一篇关于字符串为空判断方法的性能分析文章,实验结果作者已经给出,结论是使用.length==0判断的效率最高,但是文章的结尾只有一句话,感觉不够详细,所以自己写下这个文章,算一个补充和学习吧.

#慧都22周年庆大促·界面/图表报表/文档/IDE/IOT/测试等千款热门软控件火热促销中>>

偶然看到<C#中判断空字符串的3种方法性能分析 >作者:清清月儿  主页: &nbsp;         时间:2007.4.28

写的一篇关于字符串为空判断方法的性能分析文章,实验结果作者已经给出,结论是使用.length==0判断的效率最高,但是文章的结尾只有一句话,感觉不够详细,所以自己写下这个文章,算一个补充和学习吧.

程序代码执行的硬件环境:

CPU

Intel T2300 1.66GHz

内存

Kingston DDR2 667 1G

硬盘

80G 5400 8m

     测试的软件环境:

OS

Windows XP Pro

IDE

VS 2008 RTM

     测试的代码如下:

定义了3个变量,分别调用4种方法,进行100w次判断,记录测试时间:

 


  1Stopwatch sw = new Stopwatch();//实例化一个对象,记录时间
  2     &nbsp; &nbsp;    string sEmpty1 = string.Empty;//实例化3个字符串对象,赋值如下。分别作空比较试验
  3&nbsp;           string sEmpty2 = "";
  4   &nbsp;        string sEmpty3 = "StringNotEmpty";
  5           &nbsp;////////////////////////////////////////////////////Test sEmpty1///////////////////////////
  6     &nbsp;      sw.Start();//开始记录
  7&nbsp;           for (int i = 0; i <= 1000000; i++)
  8          ;  {
  9   &nbsp;&nbsp;           if (sEmpty1.Length == 0
 10     &nbsp;        ;  { }
 11   &nbsp;        }

 12&nbsp;       &nbsp;   sw.Stop();//停止记录时间
 13          &nbsp; Console.WriteLine("string.Empty Length ==&nbsp;0 Time Cost is {0}", sw.ElapsedMilliseconds);
 14  &nbsp;&nbsp;        ////////
 15    &nbsp;       sw.Reset();//重置计数器为0;
 16            sw.Start();
 17         ;   for (int i = 0; i <= 1000000; i++)
 18      &nbsp;     {
 19             &nbsp;  if (sEmpty1 == "")
 20           &nbsp;    { }
 21            }

 22            sw.Stop();
 23
 24            ;Console.WriteLine("string.Empty == \"\" Time Cost is {0}", sw.ElapsedMilliseconds);
 25         &nbsp;  ////
 26            sw.Reset();
 27            sw.Start();
 28     &nbsp;  &nbsp;   for (int i = 0; i <= 1000000; i++)
 29  ;          {
 30  &nbsp;             if (sEmpty1 == string.Empty) 
 31        &nbsp;       { }
 32      &nbsp;     }

 33            sw.Stop();
 34      &nbsp;     Console.WriteLine("string.Empty  == string.Empty Time Cost&nbsp;is {0}", sw.ElapsedMilliseconds);
 35
 36            sw.Reset();
 37            sw.Start();
 38  &nbsp;        &nbsp;for (int i = 0; i <= 1000000; i++)
 39  &nbsp;&nbsp;        {
 40       &nbsp;        if(string.IsNullOrEmpty(sEmpty1))
 41               &nbsp;{}
 42          ;  }

 43            sw.Stop();
 44  ;          Console.WriteLine("string.IsNullOrEmpty Time Cost is {0}", sw.ElapsedMilliseconds);
 45            Console.WriteLine();
 46 &nbsp; &nbsp;        ////////////////////////////////////////////////////Test sEmpty2///////////////////////////
 47            sw.Reset();
 48            sw.Start();
 49  &nbsp;         for (int i = 0; i <= 1000000; i++)
 50     &nbsp;      {
 51 &nbsp;&nbsp;             if(sEmpty2.Length == 0)
 52           &nbsp;&nbsp;   {}
 53            }

 54            sw.Stop();
 55&nbsp;           Console.WriteLine("\"\" Length == 0 Time ;Cost is {0}", sw.ElapsedMilliseconds);
 56      &nbsp;     ////////
 57            sw.Reset();
 58            sw.Start();
 59 &nbsp;     ;      for (int i = 0; i <= 1000000; i++)
 60           ; {
 61          &nbsp;     if(sEmpty2 == "")
 62      &nbsp;       &nbsp; {}
 63&nbsp;     &nbsp;     }

 64            sw.Stop();
 65        &nbsp; &nbsp; Console.WriteLine("\"\" == \"\" Time Cost is {0}", sw.ElapsedMilliseconds);
 66&nbsp;           /////
 67            sw.Reset();
 68            sw.Start();
 69&nbsp;      &nbsp;    for (int i = 0; i <= 1000000; i++)
 70   &nbsp;        {
 71&nbsp;&nbsp;              if(sEmpty2 == string.Empty)
 72           &nbsp;   &nbsp;{}
 73            }

 74            sw.Stop();
 75            Console.WriteLine("\"\"  == string.Empty Test3 Time Cost is {0}", sw.ElapsedMilliseconds);
 76          &nbsp; /////
 77            sw.Reset();
 78            sw.Start();
 79       ;    ; for (int i = 0; i <= 1000000; i++)
 80    &nbsp;       {
 81       &nbsp;        if(string.IsNullOrEmpty(sEmpty2))
 82               ; {}
 83          &nbsp; }

 84            sw.Stop();
 85     ;     &nbsp; Console.WriteLine("\"\" ;string.IsNullOrEmpty ;Time Cost is {0}", sw.ElapsedMilliseconds);
 86            Console.WriteLine();
 87          ;  ////////////////////////////////////////////////////Test sEmpty3///////////////////////////
 88            sw.Reset();
 89            sw.Start();
 90       &nbsp;    for (int i = 0; i <= 1000000; i++)
 91    ;        {
 92              ;  if(sEmpty3.Length == 0)
 93   &nbsp;&nbsp;           {}
 94&nbsp;      &nbsp;    }

 95            sw.Stop();
 96   &nbsp;        Console.WriteLine("\"StringNotEmpty\" Length == 0 Time&nbsp;Cost is {0}", sw.ElapsedMilliseconds);
 97     ;  &nbsp;    ////////
 98            sw.Reset();
 99            sw.Start();
100           &nbsp;for (int i = 0; i <= 1000000; i++)
101    &nbsp;       {
102    &nbsp;        ;   if(sEmpty3 == "")
103              &nbsp; {}
104           &nbsp;}

105            sw.Stop();
106            Console.WriteLine("\"StringNotEmpty\" == \"\" Time Cost is {0}", sw.ElapsedMilliseconds);
107            ////
108            sw.Reset();
109            sw.Start();
110     &nbsp;      for (int i = 0; i <= 1000000; i++)
111      &nbsp;&nbsp;    {
112 &nbsp;            &nbsp; if(sEmpty3 == string.Empty)
113&nbsp;               {}
114            }

115            sw.Stop();
116  ;          Console.WriteLine("\"StringNotEmpty\"&nbsp; == string.Empty Test3 Time Cost is {0}", sw.ElapsedMilliseconds);
117   ;         ////
118            sw.Reset();
119            sw.Start();
120     &nbsp;&nbsp;     for (int i = 0; i <= 1000000; i++)
121            {
122      ;          if(string.IsNullOrEmpty(sEmpty3))
123     ;           {}
124     &nbsp;    ;  }

125            sw.Stop();
126    &nbsp;       Console.WriteLine("\"StringNotEmpty\" IsNullOrEmpty Time Cost is {0}", sw.ElapsedMilliseconds);

 

代码的运行结果如下:

结果分析来看,调用string的length==0作比较,不论字符串是否为空,此方法的效率最高,此点与清清月儿的结果一致;

string的isNullOrEmpty()方法的效率基本不变,无论字符串是否有值;

== string.Empty== ""两种方法在3个变量测试的实验中效率相对较低,但是两者再和对方比较的时候会出现效率降低的情况,见上图;

    原因是什么呢?我们来看看对应的il代码:

 


 1.locals init ([0class [System]System.Diagnostics.Stopwatch sw,
 2       &nbsp;   [1string sEmpty1,
 3        &nbsp;  [2string sEmpty2,
 4  ;&nbsp;        [3string sEmpty3,
 5       &nbsp;   [4] int32 i,
 6           [5bool CS$4$0000)
 7  IL_0000:  nop
 8  IL_0001:  newobj     instance void [System]System.Diagnostics.Stopwatch::.ctor()
 9  IL_0006:  stloc.0
10  IL_0007:  ldsfld   &nbsp; string [mscorlib]System.String::Empty//将指定字段的值推送到堆栈上。 ldsfld 指令将静态(在类的所有实例中共享)字段的值推送到堆栈上。返回类型是与传递的元数据标记 field 关联的类型。
11
12  IL_000c:  stloc.1
13  IL_000d:  ldstr      ""//将对字符串的对象引用推送到堆栈上,ldstr&nbsp;指令推送对表示在元数据中存储的特定字符串的新字符串对象的对象引用(O 类型)。
14  IL_0012:  stloc.2
15  IL_0013:  ldstr      "StringNotEmpty"//将对字符串的对象引用推送到堆栈上,ldstr 指令推送对表示在元数据中存储的特定字符串的新字符串对象的对象引用(O 类型)。
16  IL_0018:  stloc.3
17  IL_0019:  ldloc.0
18

 

两者的差别由于推送到堆栈上的内容不同,前者是静态共享值推送到堆栈,后者是字符串对象的地址推送到堆栈.

造成的比较差别.另外字符串值是否相等的资料大家可以看看园子里缘清的文章,有很好的参考价值.地址:.希望大家一起交流!

谢谢!~


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@hmdbvip.cn

文章转载自:博客园

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP
PM娱乐城网络现金网站(官方)网站/网页版登录入口/手机版登录入口-最新版(已更新) PM娱乐城最大(官方)网站/网页版登录入口/手机版登录入口-最新版(已更新) 永利外围最新(官方)网站/网页版登录入口/手机版登录入口-最新版(已更新) 网络权威朗驰娱乐大全(官方)网站/网页版登录入口/手机版登录入口-最新版(已更新) 永利真人网上足球(官方)网站/网页版登录入口/手机版登录入口-最新版(已更新) 利记最火十大网(官方)网站/网页版登录入口/手机版登录入口-最新版(已更新) boyu·博鱼权威网络足球(官方)网站/网页版登录入口/手机版登录入口-最新版(已更新) PM娱乐城网上足球(官方)网站/网页版登录入口/手机版登录入口-最新版(已更新)