与string.Empty(c#)相比,性能最佳
两者之间有什么区别吗与string.Empty(c#)相比,性能最佳,c#,.net,C#,.net,两者之间有什么区别吗 if(string.Empty.Equals(text)) 及 关于性能、意外行为或可读性?这些将具有相同的性能特征 如果text为null,则带有的第二行将抛出NullReferenceException 我个人认为: if(text == string.Empty) 比您的选项更具可读性 还有内置的: if(string.IsNullOrEmpty(text)) 对于.NET 4.0: if(string.IsNullOrWhitespace(text)) 在这
if(string.Empty.Equals(text))
及
关于性能、意外行为或可读性?这些将具有相同的性能特征 如果
text
为null
,则带有的第二行将抛出NullReferenceException
我个人认为:
if(text == string.Empty)
比您的选项更具可读性
还有内置的:
if(string.IsNullOrEmpty(text))
对于.NET 4.0:
if(string.IsNullOrWhitespace(text))
在这种情况下,应该没有性能差异。这就像比较“x==y”和“y==x” 我想说,
if(text==string.Empty)
是最可读的语法,但这只是我的想法
当然,您也可以使用if(string.IsNullOrEmpty(text)){}
,或者string.IsNullOrWhiteSpace(text)
至于意外行为,这是一个非常简单的字符串比较。我无法想象你会如何从中获得意想不到的行为。我建议使用
if (string.IsNullOrEmpty(text))
{
}
因为我认为它更具可读性,这在这里是最重要的(实际上,null
值的结果不一样,但是您的第二个版本会为这个特定的测试用例抛出一个异常)
我不知道生成的IL是否有任何不同,但无论如何,这样的微优化(如果有…)显然不会使您的应用程序更快
编辑: 我只是好奇地测试了一下:
private static void Main(string[] args)
{
Stopwatch z1 = new Stopwatch();
Stopwatch z2 = new Stopwatch();
Stopwatch z3 = new Stopwatch();
int count = 100000000;
string text = "myTest";
z1.Start();
for (int i = 0; i < count; i++)
{
int tmp = 0;
if (string.Empty.Equals(text))
{
tmp++;
}
}
z1.Stop();
z2.Start();
for (int i = 0; i < count; i++)
{
int tmp = 0;
if (text.Equals(string.Empty))
{
tmp++;
}
}
z2.Stop();
z3.Start();
for (int i = 0; i < count; i++)
{
int tmp = 0;
if (string.IsNullOrEmpty(text))
{
tmp++;
}
}
z3.Stop();
Console.WriteLine(string.Format("Method 1: {0}", z1.ElapsedMilliseconds));
Console.WriteLine(string.Format("Method 2: {0}", z2.ElapsedMilliseconds));
Console.WriteLine(string.Format("Method 3: {0}", z3.ElapsedMilliseconds));
Console.ReadKey();
}
方法1和2与预期的完全相同,方法3,IMO中更具可读性的解决方案,看起来是最快的解决方案,所以请选择;) 关于性能,它们的行为相同。关于意外行为,如果text=null,第二个可能会抛出
NullReferenceException
<代码>如果(!string.IsNullOrEmpty(text))看起来更自然,但要实现同样的效果(同样的性能,同样的结果),而不会出现意外行为和NRE的可能性。它们是等价的
第一种情况:
IL_0000: nop
IL_0001: ldsfld string [mscorlib]System.String::Empty
IL_0006: ldarg.0
IL_0007: callvirt instance bool [mscorlib]System.String::Equals(string)
IL_000c: ldc.i4.0
IL_000d: ceq
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: brtrue.s <target>
IL_0000:nop
IL_0001:ldsfld字符串[mscorlib]系统。字符串::空
IL_0006:ldarg.0
IL_0007:callvirt实例bool[mscorlib]System.String::Equals(String)
IL_000c:ldc.i4.0
IL_000d:ceq
IL_000f:stloc.0
IL_0010:ldloc.0
IL_0011:brtrue.s
第二种情况:
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldsfld string [mscorlib]System.String::Empty
IL_0007: callvirt instance bool [mscorlib]System.String::Equals(string)
IL_000c: ldc.i4.0
IL_000d: ceq
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: brtrue.s <target>
IL_0000:nop
IL_0001:ldarg.0
IL_0002:ldsfld string[mscorlib]System.string::Empty
IL_0007:callvirt实例bool[mscorlib]System.String::Equals(String)
IL_000c:ldc.i4.0
IL_000d:ceq
IL_000f:stloc.0
IL_0010:ldloc.0
IL_0011:brtrue.s
您的测试显示了什么?这真的重要吗?它不太可能成为瓶颈(除非您正在排序/搜索或类似情况,在这种情况下,您仍然需要首先配置文件,然后才能得到答案),那么String.IsNullOrEmpty(text)
呢?
IL_0000: nop
IL_0001: ldsfld string [mscorlib]System.String::Empty
IL_0006: ldarg.0
IL_0007: callvirt instance bool [mscorlib]System.String::Equals(string)
IL_000c: ldc.i4.0
IL_000d: ceq
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: brtrue.s <target>
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldsfld string [mscorlib]System.String::Empty
IL_0007: callvirt instance bool [mscorlib]System.String::Equals(string)
IL_000c: ldc.i4.0
IL_000d: ceq
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: brtrue.s <target>