String相等比较测试
String类字符串比较大概有4种方法:Compare(),CompareTo(), CompareOrdinal()和Equals(). Compare()方法是CompareTo()的静态版本.而Equals()与"=="是等价的,只要使用"=="运算符,就会调用Equals()方法.CompareOrdinal()对两个字符串进行比较,不考虑本地化语言和文化.
看测试比较代码:
const int Num = 10000000;
string s1 = "abcdefgh";
string s2 = "abcdefgh";
//string s1 = "abcdefgh";
//string s2 = "amklklkl";
int end;
//Compare
int start = Environment.TickCount;
for (int i = 0; i < Num; i++)
{
string.Compare(s1, s2);
}
end=Environment.TickCount;
Console.WriteLine("Compare : " + (end - start));
//CompareTo
start = Environment.TickCount;
for (int i = 0; i < Num; i++)
{
s1.CompareTo(s2);
}
end = Environment.TickCount;
Console.WriteLine("CompareTo : " + (end - start));
//CompareOrdinal
start = Environment.TickCount;
for (int i = 0; i < Num; i++)
{
string.CompareOrdinal(s1, s2);
}
end = Environment.TickCount;
Console.WriteLine("CompareOrdinal : " + (end - start));
//静态Equals
start = Environment.TickCount;
for (int i = 0; i < Num; i++)
{
string.Equals(s1, s2);
}
end = Environment.TickCount;
Console.WriteLine("静态Equals : " + (end - start));
//实例Equals
start = Environment.TickCount;
for (int i = 0; i < Num; i++)
{
s1.Equals(s2);
}
end = Environment.TickCount;
Console.WriteLine("实例Equals : " + (end - start));
其中,s1和s2分相等和不相等两种情况. 看结果:
1.Compare方法可能会比CompareTo快一些,因为它是静态的,调用它的开销要比实例的小一些
2.CompareOrdinal要不以上两个方法夸得多,因为它不用考虑文化方面的信息,减少了系统调用开销(当然Compare有忽略文化,区域信息比较的重载,这里没做比较)
3.Equals是一个比较迅速的操作,因为比较散列码很快.
看静态Equals:
public static bool Equals(string a, string b)
{
return ((a == b) || (((a != null) && (b != null)) && EqualsHelper(a, b)));
}
实例Equals:
public bool Equals(string value)
{
if (this == null)
{
throw new NullReferenceException();
}
if (value == null)
{
return false;
}
return (object.ReferenceEquals(this, value) || EqualsHelper(this, value));
}
其中:EqualsHelper
private static unsafe bool EqualsHelper(string strA, string strB)
{
int length = strA.Length;
if (length != strB.Length)
{
return false;
}
fixed (char* chRef = &strA.m_firstChar)
{
fixed (char* chRef2 = &strB.m_firstChar)
{
char* chPtr = chRef;
char* chPtr2 = chRef2;
while (length >= 10)
{
if ((((*(((int*) chPtr)) != *(((int*) chPtr2))) || (*(((int*) (chPtr + 2))) != *(((int*) (chPtr2 + 2))))) || ((*(((int*) (chPtr + 4))) != *(((int*) (chPtr2 + 4)))) || (*(((int*) (chPtr + 6))) != *(((int*) (chPtr2 + 6)))))) || (*(((int*) (chPtr + 8))) != *(((int*) (chPtr2 + 8)))))
{
break;
}
chPtr += 10;
chPtr2 += 10;
length -= 10;
}
while (length > 0)
{
if (*(((int*) chPtr)) != *(((int*) chPtr2)))
{
break;
}
chPtr += 2;
chPtr2 += 2;
length -= 2;
}
return (length <= 0);
}
}
}