title: Performance of string.IndexOf OrdinalIgnoreCase vs CurrentCultureIgnoreCase
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3771030/string-comparison-in-dotnet-framework-4">String comparison in dotnet framework 4</a>  </p>
  </blockquote>
  
  
  
  <p>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.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>As you can see OrdinalIgnoreCase is over 6.5x slower! But without IgnoreCase Ordinal is the fastest. In <a href="http://msdn.microsoft.com/en-us/library/dd465121.aspx" rel="nofollow noreferrer">multiple places microsoft recommends</a> OrdinalIgnoreCase for the best performance. Can anyone replicate these results or explain why OrdinalIgnoreCase is going so much slower in this test?</p>
  
  <pre><code>private static void Test(string search, string key, StringComparison comparison, int trials)
  {
      var sw = Stopwatch.StartNew();
  
      for (int i = 0; i &lt; 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);
  }
  </code></pre>