擂台:改进 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);
}
...全文
2331 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);
}
}
}
gggscfaii 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)
内容概要:本文围绕【复现IEEE二区文献】基于状态空间表示与组件连接法的构网型逆变器统一小信号建模框架研究,提出了一种适用于多控制策略构网型逆变器的小信号建模方法。该方法结合状态空间建模理论与组件连接法(Component Connection Method),实现了对复杂逆变器系统动态特性的精确描述,尤其适用于分析系统在不同运行工况下的稳定性。通过Matlab代码实现,构建了包含控制环路、滤波器、锁相环等关键模块的完整小信号模型,并利用特征值分析与模态参与度评估系统稳定性,解决了传统建模方法在面对多时间尺度、强耦合非线性控制结构时建模困难、精度不足的问题。研究还探讨了关键控制参数对系统稳定边界的影响,为构网型逆变器的参数整定与优化设计提供了理论依据和技术支撑。; 适合人群:具备电力电子、自动控制理论基础,从事新能源发电、微电网、电力系统稳定性研究的研究生、科研人员及工程技术人员,尤其适合致力于高水平期刊论文复现与仿真实践的科研工作者。; 使用场景及目标:① 掌握构网型逆变器统一小信号建模的核心方法,复现IEEE二区高水平文献成果;② 理解状态空间法与组件连接法在复杂电力电子系统建模中的集成应用;③ 利用Matlab进行特征值分析与稳定性判据研究,支撑科研仿真与论文写作;④ 为多逆变器并联系统、微电网稳定性分析等课题提供建模工具与技术路线参考。; 阅读建议:建议读者结合Matlab代码与文中建模流程逐步调试,重点关注各子模块状态方程的推导与系统整体矩阵组装过程,深入理解控制参数对系统极点分布的影响规律,建议配合其他稳定性分析案例进行对比学习以加深理解。
内容概要:WiFiSupply斯普莱WFS7000XB52AX5G是一款高性能、工业级的5G全网通+双频WIFI6无线AP基站,支持5G与工业大功率Wi-Fi链路无缝切换与带宽聚合,具备高达2974Mbps的双频带宽和1000Mbps以上的实际吞吐量。设备采用MIMO 4T4R架构,支持OFDMA、AI智能FastRoaming等先进技术,实现低至20ms的漫游切换延迟和超远传输距离(点对点桥接可达150公里以上)。产品具备IP68防水、-45~85℃宽温运行、多重看门狗、防浪涌、防静电等工业级防护设计,适用于复杂恶劣环境。支持胖瘦AP模式、多种认证与管理方式(含云平台、API接口),并集成丰富的网络安全与QoS功能,适用于智慧物流、工业4.0、巡检机器人、无人机、车载船舶等多种场景。; 适合人群:从事工业物联网、智能制造、自动化物流、智能交通等领域的网络工程师、系统集成商、设备制造商及技术决策人员。; 使用场景及目标:①实现移动设备(如AGV/AMR/巡检机器人)在高速移动中的零丢包、低延迟无线漫游;②在无固定网络覆盖区域构建高带宽、高可靠的远距离无线通信链路;③为5G与工业Wi-Fi双链路备份与聚合提供一体化解决方案,保障关键业务连续性;④在复杂电磁环境下实现稳定、安全的数据传输与远程控制。; 阅读建议:本产品技术参数详尽,建议结合具体应用场景重点关注其无线性能、漫游技术、供电方式与安装选项,并参考官方提供的管理平台与API接口文档进行系统集成与远程运维规划。

111,128

社区成员

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

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

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