有谁知道为什么 C# (.NET) 的StartsWith函数比IsPrefix慢很多?
Frantisek
问问题
8987 次
4 回答
35
我认为它主要是获取线程的当前文化。
如果您更改 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,culture): 5568ms
- 比较.IsPrefix(s1, s2): 5200ms
- s1.StartsWith(s2, StringComparison.Ordinal): 1393ms
显然这是因为它实际上只是在每个点比较 16 位整数,这非常便宜。如果您不想要对文化敏感的检查,并且性能对您来说特别重要,那么这就是我要使用的重载。
于 2009-04-04T21:49:42.773 回答
7
好问题; 进行测试,我得到:
9156ms; chk: 50000000
6887ms; chk: 50000000
试验台:
using System;
using System.Diagnostics;
using System.Globalization;
class Program
{
static void Main()
{
string s1 = "abcdefghijklmnopqrstuvwxyz", s2 = "abcdefg";
const int LOOP = 50000000;
int chk = 0;
Stopwatch watch = Stopwatch.StartNew();
for (int i = 0; i < LOOP; i++)
{
if (s1.StartsWith(s2)) chk++;
}
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds + "ms; chk: " + chk);
chk = 0;
watch = Stopwatch.StartNew();
CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;
for (int i = 0; i < LOOP; i++)
{
if (ci.IsPrefix(s1, s2)) chk++;
}
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds + "ms; chk: " + chk);
}
}
于 2009-04-04T21:43:40.607 回答
6
StartsWith 在内部调用 IsPrefix。它在调用 IsPrefix 之前分配文化信息。
于 2009-04-04T21:39:02.223 回答
1
查看 IsPrefix 的来源。问题是 - 在某些情况下,它会比StartsWith
仅仅因为它实际上使用 StartsWith 并且只执行很少的操作而变慢。
[System.Security.SecuritySafeCritical] // auto-generated
public unsafe virtual bool IsPrefix(String source, String prefix, CompareOptions options)
{
if (source == null || prefix == null) {
throw new ArgumentNullException((source == null ? "source" : "prefix"),
Environment.GetResourceString("ArgumentNull_String"));
}
Contract.EndContractBlock();
int prefixLen = prefix.Length;
if (prefixLen == 0)
{
return (true);
}
if (options == CompareOptions.OrdinalIgnoreCase)
{
return source.StartsWith(prefix, StringComparison.OrdinalIgnoreCase);
}
if (options == CompareOptions.Ordinal)
{
return source.StartsWith(prefix, StringComparison.Ordinal);
}
if ((options & ValidIndexMaskOffFlags) != 0) {
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");
}
// to let the sorting DLL do the call optimization in case of Ascii strings, we check if the strings are in Ascii and then send the flag RESERVED_FIND_ASCII_STRING to
// the sorting DLL API SortFindString so sorting DLL don't have to check if the string is Ascii with every call to SortFindString.
return (InternalFindNLSStringEx(
m_dataHandle, m_handleOrigin, m_sortName,
GetNativeCompareFlags(options) | Win32Native.FIND_STARTSWITH | ((source.IsAscii() && prefix.IsAscii()) ? RESERVED_FIND_ASCII_STRING : 0),
source, source.Length, 0, prefix, prefix.Length) > -1);
}
于 2013-06-22T08:32:04.900 回答