I noticed a performance problem on my machine in a UI app that is doing lots of string comparisons to do filtering of large lists. I tracked the issue down to using OrdinalIgnoreCase in a call to string.IndexOf. The following benchmarks were run in Release without the debugger attached, it's a 4.0 project built in VS 2010, windows 7, I do have the 4.5 beta installed on this machine, I'm not sure if that would affect this.

1.190 seconds for OrdinalIgnoreCase
0.178 seconds for CurrentCultureIgnoreCase
0.175 seconds for InvariantCultureIgnoreCase

0.101 seconds for Ordinal
0.132 seconds for CurrentCulture
0.126 seconds for InvariantCulture

1.176 seconds for OrdinalIgnoreCase
0.189 seconds for CurrentCultureIgnoreCase
0.183 seconds for InvariantCultureIgnoreCase

0.104 seconds for Ordinal
0.138 seconds for CurrentCulture
0.127 seconds for InvariantCulture

As you can see OrdinalIgnoreCase is over 6.5x slower! But without IgnoreCase Ordinal is the fastest. In multiple places microsoft recommends OrdinalIgnoreCase可获得最佳性能。任何人都可以复制这些结果或解释为什么OrdinalIgnoreCase在此测试中速度要慢得多吗?

private static void Test(string search, string key, StringComparison comparison, int trials)
{
    var sw = Stopwatch.StartNew();

    for (int i = 0; i < trials; i++)
    {
        search.IndexOf(key, comparison);
    }

    Console.WriteLine("{0:0.000} seconds for {1}", sw.ElapsedMilliseconds / 1000.0, comparison);
}


static void Main(string[] args)
{
    int trials = 1000000;
    var search = Guid.NewGuid().ToString("N");
    var key = "34";

    Test(search, key, StringComparison.OrdinalIgnoreCase, trials);
    Test(search, key, StringComparison.CurrentCultureIgnoreCase, trials);
    Test(search, key, StringComparison.InvariantCultureIgnoreCase, trials);
    Test(search, key, StringComparison.Ordinal, trials);
    Test(search, key, StringComparison.CurrentCulture, trials);
    Test(search, key, StringComparison.InvariantCulture, trials);

    Test(search, key, StringComparison.OrdinalIgnoreCase, trials);
    Test(search, key, StringComparison.CurrentCultureIgnoreCase, trials);
    Test(search, key, StringComparison.InvariantCultureIgnoreCase, trials);
    Test(search, key, StringComparison.Ordinal, trials);
    Test(search, key, StringComparison.CurrentCulture, trials);
    Test(search, key, StringComparison.InvariantCulture, trials);
}

最佳答案

这显然是.net 4中已知的性能问题,我发现this bug entry on connect.microsoft.com

并有回应



我不确定下一个版本是什么,我只喜欢使用InvariantCultureIgnoreCase代替

10-04 10:14