Search

Performance of string.IndexOf(string, StringComparison.OrdinalIgnoreCase) is worse than InvariantCultureIgnoreCase by mspi1

Closed
as Fixed Help for as Fixed

6
0
Sign in
to vote
Type: Bug
ID: 699078
Opened: 11/4/2011 7:08:51 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
2
User(s) can reproduce this bug
The performance of string.IndexOf(string, StringComparison.OrdinalIgnoreCase) is about 10 times worse than string.IndexOf(string, StringComparison.InvariantCultureIgnoreCase) in a simple szenario.

According to documentation and best practice advice OrdinalIgnoreCase should be faster, e.g. http://msdn.microsoft.com/en-us/library/dd465121.aspx.
In .Net 2.0 OrdinalIgnoreCase was faster than InvariantCultureIgnoreCase, the behavior changed with .Net 4.0.

See also:
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/51c5dcc1-190a-4de7-a62f-a7d967bc517e/
http://stackoverflow.com/questions/3771030/string-comparison-in-dotnet-framework-4/3776360
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling version

.NET Framework 4

Steps to reproduce

Create the following console application and let it run.

Results on my machine:

//// .Net 4
// InvariantCultureIgnoreCase: 00:00:09.1397865
// OrdinalIgnoreCase:             00:01:51.0170519

//// .Net 2
// InvariantCultureIgnoreCase: 00:00:07.0405907
// OrdinalIgnoreCase:             00:00:07.0258267


using System;
using System.Diagnostics;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
     string source = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
     string pattern = "in";

     int warmupcount = 10;

     for (int i = 0; i < warmupcount; i++)
     {
        int result = source.IndexOf(pattern, StringComparison.InvariantCultureIgnoreCase);
     }

     for (int i = 0; i < warmupcount; i++)
     {
        int result = source.IndexOf(pattern, StringComparison.OrdinalIgnoreCase); ;
     }

     int count = 10000000;

     Stopwatch watch = Stopwatch.StartNew();
     for (int i = 0; i < count; i++)
     {
        int result = source.IndexOf(pattern, StringComparison.InvariantCultureIgnoreCase);
     }
     watch.Stop();
     Console.WriteLine("{0, -30}: {1}", "InvariantCultureIgnoreCase", watch.Elapsed);

     watch.Reset();
     watch.Start();
     for (int i = 0; i < count; i++)
     {
        int result = source.IndexOf(pattern, StringComparison.OrdinalIgnoreCase);
     }
     watch.Stop();
     Console.WriteLine("{0, -30}: {1}", "OrdinalIgnoreCase", watch.Elapsed);

     Console.ReadLine();
    }
}
}

Product Language

German

Operating System

Windows 7

Operating System Language

German

Actual results

In .Net 4 OrdinalIgnoreCase is magnitudes slower than InvariantCultureIgnoreCase.

Expected results

OrdinalIgnoreCase should be as fast or faster than InvariantCultureIgnoreCase.
Both variants should be as fast as in .Net 2.
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 2/10/2012 at 11:43 AM
We were able to repro this issue. The issue has been resolved and the fix will be in the next release. Thank you for you feedback.
Posted by MS-Moderator07 [Feedback Moderator] on 11/6/2011 at 10:27 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.


Posted by MS-Moderator07 on 11/6/2011 at 6:37 PM
Thank you for your feedback, We are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly (http://support.microsoft.com/)
Sign in to post a workaround.