Compare the performance of string comparison methods in C#


This example uses the following code to make four strings:


It then uses several methods for comparing the first string with the others. For example, it uses the following code to compare the strings by using ==.

start_time = DateTime.Now;
for (int i = 1; i <= iterations; i++)
    if (value0 == value1) { }
    if (value0 == value2) { }
    if (value0 == value3) { }
elapsed = DateTime.Now - start_time;
txtEqEq.Text = elapsed.TotalSeconds.ToString("0.000") + " sec";

The following code fragment shows the statements the program uses to perform the different comparisons. The last three methods perform case-insensitive comparisons. (I've combined them all to one place here. They are not all together in the code.)

if (value0 == value1) { }
if (String.Compare(value0, value1, false) == 0) { }
if (value0.Equals(value1)) { }

if (String.Compare(value0, value1, true) == 0) { }
if (value0.Equals(value1,
    StringComparison.CurrentCultureIgnoreCase)) { }
if (value0.ToLower() == value1) { }

You can see from the results that == gave the fastest performance, followed closely by .Equals. The String.Compare method was much slower.

I think the reason == is so fast is due to the way C# handles strings. When you create a string in .NET, it is interned and placed in an intern pool. Later if you create another string containing the same text, the new string refers to the same instance in the intern pool. That makes it relatively quick and easy to compare two strings.

Among the case-insensitive tests, String.Equals gave the fastest performance. Converting the test string to lower case for every iteration was quite slow, however if you need to compare a string to many other values and you can convert the test string to lower case only once before all of the tests, then the ToLower method basically converts into the == case and performance will be much better. If the run time is just an aggregate over a large number of comparisons with different test values, then that technique won't work.

If you need to look up a particular string, rather than comparing a test value to a bunch of other values, consider using a Dictionary.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in performance, strings and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.