I recently had an app that I used the VSTS Profiler on to find that a significant portion of time was spent comparing some long strings. I was using String.Equals(string a, string b, StringComparison.InvariantCultureIgnoreCase) since I didn’t care about case or culture. I switched to using OrdinalIgnoreCase and got a huge 5x performance gain! So that begged the question, just how long do the various string comparison algorithms comparatively take?
OrdinalIgnoreCase was the fastest. Ordinal was almost 2x as long. The others were about 5x the cost.
The “==” operator (“a” == “b”) uses Ordinal by default. So if case sensitivity isn’t an issue, you can get a 2x performance gain by using OrdinalIgnoreCase.
To perform the calculation: Comparing strings 200 characters in length made up of random characters 4,000,000 times with each of the StringComparison values, all done 4 times and averaged. On a 1.8GHz Dell D610 w/ 2GB RAM using .NET 3.5 Beta2. Download StringComparisonTest.zip