111,079
社区成员




static int StringCompare(string s1, string s2)
{
return String.CompareOrdinal(s1, s2);
}
static double CompareLoop(string s1, string s2, int count, out int result)
{
result = 0;
DateTime start = DateTime.Now;
for (int i = 0; i < count; i++)
{
result += StringCompare(s1, s2);
}
return (DateTime.Now - start).TotalMilliseconds;
}
static void Main(string[] args)
{
string s0 = "A quick brown fox jumps over a lady jog. ";
string s1 = String.Empty;
for (int i = 0; i < 10; i++)
{
s1 = s1 + s0;
}
string s2 = s1 + "A";
int count = 10 * 1000 * 1000;
int result1;
int result2;
double t1 = CompareLoop("a", "b", count * 10, out result1);
double t2 = CompareLoop(s1, s2, count, out result2);
Console.WriteLine("Short {0} ms {1} ", t1, result1);
Console.WriteLine("Long {0} ms {1} ", t2, result2);
Console.WriteLine("Total time {0} ms", t1 + t2);
}
private static unsafe int StringCompare(string left, string right)
{
if (left != null)
{
if (right != null)
{
int value = left.Length - right.Length, length = value <= 0 ? left.Length : right.Length;
fixed (char* leftFixed = left, rightFixed = right)
{
byte* leftStart = (byte*)leftFixed, rightStart = (byte*)rightFixed;
while (length >= 8)
{
uint code = (*(uint*)leftStart ^ *(uint*)rightStart) | (*(uint*)(leftStart + 4) ^ *(uint*)(rightStart + 4));
if ((code | (*(uint*)(leftStart + 8) ^ *(uint*)(rightStart + 8))
| (*(uint*)(leftStart + 12) ^ *(uint*)(rightStart + 12))) != 0)
{
if (code == 0)
{
leftStart += 8;
rightStart += 8;
}
if (*(uint*)leftStart == *(uint*)rightStart)
{
leftStart += 4;
rightStart += 4;
}
value = (int)*(ushort*)leftStart - *(ushort*)rightStart;
return value == 0 ? (int)*(ushort*)(leftStart += 2) - *(ushort*)(rightStart += 2) : value;
}
length -= 8;
leftStart += 16;
rightStart += 16;
}
if ((length & 4) != 0)
{
uint code = *(uint*)leftStart ^ *(uint*)rightStart;
if ((code | (*(uint*)(leftStart + 4) ^ *(uint*)(rightStart + 4))) != 0)
{
if (code == 0)
{
leftStart += 4;
rightStart += 4;
}
value = (int)*(ushort*)leftStart - *(ushort*)rightStart;
return value == 0 ? (int)*(ushort*)(leftStart += 2) - *(ushort*)(rightStart += 2) : value;
}
leftStart += 8;
rightStart += 8;
}
if ((length & 2) != 0)
{
int code = (int)*(ushort*)leftStart - *(ushort*)rightStart;
if (code != 0) return code;
code = (int)*(ushort*)(leftStart + 2) - *(ushort*)(rightStart + 2);
if (code != 0) return code;
leftStart += 4;
rightStart += 4;
}
if ((length & 1) != 0)
{
int code = (int)*(ushort*)leftStart - *(ushort*)rightStart;
if (code != 0) return code;
}
}
return value;
}
return 1;
}
return right != null ? -1 : 0;
}
private static unsafe int CompareOrdinalHelper(string strA, string strB)
{
int num = Math.Min(strA.Length, strB.Length);
int num2 = -1;
fixed (char* chRef = &strA.m_firstChar)
{
fixed (char* chRef2 = &strB.m_firstChar)
{
char* chPtr = chRef;
char* chPtr2 = chRef2;
while (num >= 10)
{
if (*(((int*)chPtr)) != *(((int*)chPtr2)))
{
num2 = 0;
break;
}
if (*(((int*)(chPtr + 2))) != *(((int*)(chPtr2 + 2))))
{
num2 = 2;
break;
}
if (*(((int*)(chPtr + 4))) != *(((int*)(chPtr2 + 4))))
{
num2 = 4;
break;
}
if (*(((int*)(chPtr + 6))) != *(((int*)(chPtr2 + 6))))
{
num2 = 6;
break;
}
if (*(((int*)(chPtr + 8))) != *(((int*)(chPtr2 + 8))))
{
num2 = 8;
break;
}
chPtr += 10;
chPtr2 += 10;
num -= 10;
}
if (num2 == -1)
{
goto Label_00F1;
}
chPtr += num2;
chPtr2 += num2;
int num3 = chPtr[0] - chPtr2[0];
if (num3 != 0)
{
return num3;
}
return (chPtr[1] - chPtr2[1]);
Label_00D7:
if (*(((int*)chPtr)) != *(((int*)chPtr2)))
{
goto Label_00F5;
}
chPtr += 2;
chPtr2 += 2;
num -= 2;
Label_00F1:
if (num > 0)
{
goto Label_00D7;
}
Label_00F5:
if (num > 0)
{
int num4 = chPtr[0] - chPtr2[0];
if (num4 != 0)
{
return num4;
}
return (chPtr[1] - chPtr2[1]);
}
return (strA.Length - strB.Length);
}
}
}