写的一篇关于字符串为空判断方法的性能分析文章,实验结果作者已经给出,结论是使用.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 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 highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
--> 1.locals init ([0] class [system]system.diagnostics.stopwatch sw,
2 [1] string sempty1,
3 [2] string sempty2,
4 [3] string sempty3,
5 [4] int32 i,
6 [5] bool 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 string [mscorlib]system.string::empty//将指定字段的值推送到堆栈上。 ldsfld 指令将静态(在类的所有实例中共享)字段的值推送到堆栈上。返回类型是与传递的元数据标记 field 关联的类型。
11
12 il_000c: stloc.1
13 il_000d: ldstr ""//将对字符串的对象引用推送到堆栈上,ldstr 指令推送对表示在元数据中存储的特定字符串的新字符串对象的对象引用(o 类型)。
14 il_0012: stloc.2
15 il_0013: ldstr "stringnotempty"//将对字符串的对象引用推送到堆栈上,ldstr 指令推送对表示在元数据中存储的特定字符串的新字符串对象的对象引用(o 类型)。
16 il_0018: stloc.3
17 il_0019: ldloc.0
18
两者的差别由于推送到堆栈上的内容不同,前者是静态共享值推送到堆栈,后者是字符串对象的地址推送到堆栈.
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15783504/viewspace-557536/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/15783504/viewspace-557536/