有谁知道为什么 C# (.NET) 的以。。开始 http://msdn.microsoft.com/en-us/library/system.string.startswith.aspx函数比IsPrefix http://msdn.microsoft.com/en-us/library/system.globalization.compareinfo.isprefix.aspx?
我认为这主要是获取线程当前的文化。
如果您将 Marc 的测试更改为使用这种形式String.StartsWith
:
Stopwatch watch = Stopwatch.StartNew();
CultureInfo cc = CultureInfo.CurrentCulture;
for (int i = 0; i < LOOP; i++)
{
if (s1.StartsWith(s2, false, cc)) chk++;
}
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds + "ms; chk: " + chk);
它更接近了。
如果你使用s1.StartsWith(s2, StringComparison.Ordinal)
它比使用快很多CompareInfo.IsPrefix
(取决于CompareInfo
当然)。在我的盒子上,结果是(不科学):
- s1.StartsWith(s2): 6914ms
- s1.StartsWith(s2, false, 文化): 5568ms
- 比较.IsPrefix(s1, s2): 5200ms
- s1.StartsWith(s2, StringComparison.Ordinal): 1393ms
显然,这是因为它实际上只是在每个点比较 16 位整数,这非常便宜。如果你don't想要文化敏感的检查,and性能对您来说特别重要,这就是我要使用的重载。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)