ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 【Frank.Xu】.net深入学习笔记(2):c#中判断空字符串的4种方法性能比较与分析

【Frank.Xu】.net深入学习笔记(2):c#中判断空字符串的4种方法性能比较与分析

原创 Linux操作系统 作者:依旧伊利丹 时间:2009-02-24 10:06:59 0 删除 编辑

写的一篇关于字符串为空判断方法的性能分析文章,实验结果作者已经给出,结论是使用.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次判断,记录测试时间:

 

code
  1stopwatch sw = new stopwatch();//实例化一个对象,记录时间
  2            string sempty1 = string.empty;//实例化3个字符串对象,赋值如下。分别作空比较试验
  3            string sempty2 = "";
  4            string sempty3 = "stringnotempty";
  5            /**/////////////////////////////////////////////////////test sempty1///////////////////////////
  6            sw.start();//开始记录
  7            for (int i = 0; i <= 1000000; i++)
  8            {
  9                if (sempty1.length == 0)
 10                { }
 11            }
 12            sw.stop();//停止记录时间
 13            console.writeline("string.empty length == 0 time cost is {0}", sw.elapsedmilliseconds);
 14            /**/////////
 15            sw.reset();//重置计数器为0;
 16            sw.start();
 17            for (int i = 0; i <= 1000000; i++)
 18            {
 19                if (sempty1 == "")
 20                { }
 21            }
 22            sw.stop();
 23
 24            console.writeline("string.empty == \"\" time cost is {0}", sw.elapsedmilliseconds);
 25            /**/////
 26            sw.reset();
 27            sw.start();
 28            for (int i = 0; i <= 1000000; i++)
 29            {
 30                if (sempty1 == string.empty)
 31                { }
 32            }
 33            sw.stop();
 34            console.writeline("string.empty  == string.empty time cost is {0}", sw.elapsedmilliseconds);
 35
 36            sw.reset();
 37            sw.start();
 38            for (int i = 0; i <= 1000000; i++)
 39            {
 40                if(string.isnullorempty(sempty1))
 41                {}
 42            }
 43            sw.stop();
 44            console.writeline("string.isnullorempty time cost is {0}", sw.elapsedmilliseconds);
 45            console.writeline();
 46            /**/////////////////////////////////////////////////////test sempty2///////////////////////////
 47            sw.reset();
 48            sw.start();
 49            for (int i = 0; i <= 1000000; i++)
 50            {
 51                if(sempty2.length == 0)
 52                {}
 53            }
 54            sw.stop();
 55            console.writeline("\"\" length == 0 time cost is {0}", sw.elapsedmilliseconds);
 56            /**/////////
 57            sw.reset();
 58            sw.start();
 59             for (int i = 0; i <= 1000000; i++)
 60            {
 61                if(sempty2 == "")
 62                {}
 63            }
 64            sw.stop();
 65            console.writeline("\"\" == \"\" time cost is {0}", sw.elapsedmilliseconds);
 66            /**//////
 67            sw.reset();
 68            sw.start();
 69            for (int i = 0; i <= 1000000; i++)
 70            {
 71                if(sempty2 == string.empty)
 72                {}
 73            }
 74            sw.stop();
 75            console.writeline("\"\"  == string.empty test3 time cost is {0}", sw.elapsedmilliseconds);
 76            /**//////
 77            sw.reset();
 78            sw.start();
 79            for (int i = 0; i <= 1000000; i++)
 80            {
 81                if(string.isnullorempty(sempty2))
 82                {}
 83            }
 84            sw.stop();
 85            console.writeline("\"\" string.isnullorempty time cost is {0}", sw.elapsedmilliseconds);
 86            console.writeline();
 87            /**/////////////////////////////////////////////////////test sempty3///////////////////////////
 88            sw.reset();
 89            sw.start();
 90            for (int i = 0; i <= 1000000; i++)
 91            {
 92                if(sempty3.length == 0)
 93                {}
 94            }
 95            sw.stop();
 96            console.writeline("\"stringnotempty\" length == 0 time cost is {0}", sw.elapsedmilliseconds);
 97            /**/////////
 98            sw.reset();
 99            sw.start();
100            for (int i = 0; i <= 1000000; i++)
101            {
102                if(sempty3 == "")
103                {}
104            }
105            sw.stop();
106            console.writeline("\"stringnotempty\" == \"\" time cost is {0}", sw.elapsedmilliseconds);
107            /**/////
108            sw.reset();
109            sw.start();
110            for (int i = 0; i <= 1000000; i++)
111            {
112                if(sempty3 == string.empty)
113                {}
114            }
115            sw.stop();
116            console.writeline("\"stringnotempty\"  == string.empty test3 time cost is {0}", sw.elapsedmilliseconds);
117            /**/////
118            sw.reset();
119            sw.start();
120            for (int i = 0; i <= 1000000; i++)
121            {
122                if(string.isnullorempty(sempty3))
123                {}
124            }
125            sw.stop();
126            console.writeline("\"stringnotempty\" isnullorempty time cost is {0}", sw.elapsedmilliseconds);

代码的运行结果如下:

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

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

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

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

 

code

 

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

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15783504/viewspace-557536/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-09-23

  • 博文量
    26
  • 访问量
    93955