C# (.NET) のStartsWith関数がIsPrefixよりもかなり遅い理由を知っている人はいますか?
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, カルチャ): 5568ms
- compare.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 に答える