c#中提供了相当丰富的方法或属性来判断一个字符是否为空,常用的方法有以下6种
1. strtest== ""
2. strtest.equals("")
3. strtest== string.empty
4. strtest.equals(string.empty)
5. strtest.length == 0
6. string.isnullorempty(strtest)
为了对以上6种方法的效率,有个直观的感受,我特意编写了以下的测试代码:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
using system;
namespace strtest
{
class program
{
//定义3个字符串 以便测试在多种情况下 下面6种判断方法的速度
public static string strtest01 = "";
public static string strtest02 = string.empty;
public static string strtest03 = "0123456789";
public static datetime start, end; //定义开始时间 和 结束时间
public static timespan ts; //定义两个时间的间隔
//**********************对strtest使用6种测试方法*****************************
public static void test(string strtest)
{
//string == ""
start = datetime.now;
for (int counter = 0; counter <= 100000000; counter++)
{
if (strtest == "")
{
}
}
end = datetime.now;
ts = end - start;
console.writeline("string == /"/" 时间消耗为 " + ts.totalseconds + " 秒");
//string.equals("")
start = datetime.now;
for (int counter = 0; counter <= 100000000; counter++)
{
if (strtest.equals(""))
{
}
}
end = datetime.now;
ts = end - start;
console.writeline("string.equals(/"/") 时间消耗为 " + ts.totalseconds + " 秒");
//string == stirng.empty
start = datetime.now;
for (int counter = 0; counter <= 100000000; counter++)
{
if (strtest == string.empty)
{
}
}
end = datetime.now;
ts = end - start;
console.writeline("string == string.empty 时间消耗为 " + ts.totalseconds + " 秒");
//string.equals(string.empty)
start = datetime.now;
for (int counter = 0; counter <= 100000000; counter++)
{
if (strtest.equals(string.empty))
{
}
}
end = datetime.now;
ts = end - start;
console.writeline("string.equals(string.empty) 时间消耗为 " + ts.totalseconds + " 秒");
//string.length == 0
start = datetime.now;
for (int counter = 0; counter <= 100000000; counter++)
{
if (strtest.length == 0)
{
}
}
end = datetime.now;
ts = end - start;
console.writeline("string.length == 0 时间消耗为 " + ts.totalseconds + " 秒");
//string.isnullorempty(string)
start = datetime.now;
for (int counter = 0; counter <= 100000000; counter++)
{
if (string.isnullorempty(strtest))
{
}
}
end = datetime.now;
ts = end - start;
console.writeline("string.isnullorempty(string) 时间消耗为 " + ts.totalseconds + " 秒" + "/n");
}
static void main(string[] args)
{
console.writeline("=======================================");
console.writeline("strtest = /"/" 的5种测试结果");
console.writeline("=======================================");
test(strtest01);
console.writeline("=======================================");
console.writeline("strtest = string.emtpy 的5种测试结果");
console.writeline("=======================================");
test(strtest02);
console.writeline("=======================================");
console.writeline("strtest = /"0123456789/" 的5种测试结果");
console.writeline("=======================================");
test(strtest03);
console.readline(); //等待键盘的输入 作用:使屏幕暂停在此处
}
}
}
|
我把能关的软件都关闭掉了 尽可能的屏蔽掉系统影响 并且让6种方法都运行了1亿次
第一次的截图:

第二次的截图:

从以上可以看出:字符串在三种情况下,string.length == 0的效率无疑是最高的。
总结
1. strtest== "" 不推荐使用,只能判断“值为空字符串”的字符串变量,而且效率比较低。
2. strtest.equals("") 不推荐使用,同 1。
3. strtest== string.empty 不推荐使用,只能判断“值为null”的字符串变量,而且效率低。
4. strtest.equals(string.empty) 不推荐使用,同 3。
5. strtest.length == 0 这种方式,我不怎么喜欢用,不推荐使用。在自己的实际测试中,确实能证明这种判断方式的执行效率最高,但要使用它你必须保证字符串不null,如果为null就会报出异常。
6. string.isnullorempty(strtest) 这种方法是我最喜欢用的,它不但一次性能判断"空的字符串变量",还能判断“值为空字符串的变量”,并且还可以让代码简洁美观。判断的效率也不算低。








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。