擂台:改进 String.CompareOrdinal 性能

FengYuanMSFT 2011-03-27 05:59:52
加精
重新实现 String.CompareOrdinal,用 C#,不能调用 native code, 使下面程序最快 (Total time),提供前后数据

        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);
}
...全文
2113 108 打赏 收藏 转发到动态 举报
写回复
用AI写文章
108 条回复
切换为时间正序
请发表友善的回复…
发表回复
showjim 2012-04-17
  • 打赏
  • 举报
回复
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;
}
「已注销」 2011-07-07
  • 打赏
  • 举报
回复
哥们能结贴了不....
「已注销」 2011-04-20
  • 打赏
  • 举报
回复
[Quote=引用 101 楼 viki117 的回复:]
……
[/Quote]

fixed (char* chRef2 = &strB.m_firstChar)
楼上的m_firstChar是哪里来的? 这个是private的字段你肿么能用?
viki117 2011-04-20
  • 打赏
  • 举报
回复

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);
}
}
}
yhnujm##33 2011-04-18
  • 打赏
  • 举报
回复
牛逼!!!!!!
s1308842 2011-04-18
  • 打赏
  • 举报
回复
不是很明白呀。
张见 2011-04-18
  • 打赏
  • 举报
回复
过来 学习下
abs102 2011-04-18
  • 打赏
  • 举报
回复
这么长。。。
zlcp520 2011-04-18
  • 打赏
  • 举报
回复
内容存入剪贴
ktei2008 2011-04-17
  • 打赏
  • 举报
回复
比这个能比出什么来?效率差别不在于这个
KOSymbian 2011-04-17
  • 打赏
  • 举报
回复
[Quote=引用 67 楼 ktei2008 的回复:]
比这个能比出什么来?效率差别不在于这个
[/Quote]

厉害啊!!!!
petrie 2011-04-17
  • 打赏
  • 举报
回复
RLib 2011-04-17
  • 打赏
  • 举报
回复
呵呵。。。。。。。。。。用系统的
yunzhenwang 2011-04-17
  • 打赏
  • 举报
回复
学习中,不错不错
yaojianqiyinshan 2011-04-16
  • 打赏
  • 举报
回复
哎,太复杂
huwei001982 2011-04-16
  • 打赏
  • 举报
回复
居然可以用 unsafe 指针, 不是不能调用 native code 吗
dcba987654 2011-04-16
  • 打赏
  • 举报
回复
哎,太复杂

FengYuanMSFT 2011-04-16
  • 打赏
  • 举报
回复
if (*((long*)pa) != *((long*)pb))
{
return charCount;
}

return charCount 是错的,应该看第一个字符的差。
threenewbee 2011-04-16
  • 打赏
  • 举报
回复
mark下,回头来看。

不用Native Code有难度。
rabbit342 2011-04-16
  • 打赏
  • 举报
回复
study~
加载更多回复(41)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧