TheDeveloperBlog.com

Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

C# String Performance

This C# example program tests the performance of comparing strings with different characters.

String performance. Length and character composition influence string performance.

We find out if shorter strings are faster to compare—and learn how to make string comparisons faster. We measure one aspect of the performance of strings.

Note: Unequal strings were compared to each other. Different characters near the start were faster.

Benchmark of string Equals on different input

String ==, loop 1:  669 ms [fastest]
String ==, loop 2:  763 ms
String ==, loop 3: 1019 ms

Example. First, the kind of code we compare here involves two dynamic string variables. You can use string.Equals or "==" on these two strings. We use no string literals, which would be interned, placed in the executable.

string.Intern

Test program: C#

using System;
using System.Diagnostics;

class Program
{
    static void Main()
    {
	string a1 = string.Format("{0}{1}", 1, 1);
	string a2 = string.Format("{0}{1}", 1, 2);

	string b1 = string.Format("{0}{1}{2}{3}{4}{5}", 1, 1, 1, 1, 1, 1);
	string b2 = string.Format("{0}{1}{2}{3}{4}{5}", 1, 1, 1, 2, 2, 2);

	string c1 = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}",
	    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
	string c2 = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}",
	    1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2);

	const int m = 100000000;
	Stopwatch s1 = Stopwatch.StartNew();
	for (int i = 0; i < m; i++)
	{
	    if (a1 == a2)
	    {
	    }
	}
	s1.Stop();
	Stopwatch s2 = Stopwatch.StartNew();
	for (int i = 0; i < m; i++)
	{
	    if (b1 == b2)
	    {
	    }
	}
	s2.Stop();
	Stopwatch s3 = Stopwatch.StartNew();
	for (int i = 0; i < m; i++)
	{
	    if (c1 == c2)
	    {
	    }
	}
	s3.Stop();
	Console.WriteLine("{0},{1},{2}", s1.ElapsedMilliseconds,
	    s2.ElapsedMilliseconds, s3.ElapsedMilliseconds);
	Console.Read();
    }
}

The first part builds up six strings. Two strings are each 2 chars, and the second character is different. The second two strings are each 6 chars, and the fourth character is different. And the final two strings are each 12 chars.

The three loops each run 100 million times. The first loop contains the op_Equality instruction for the first two strings. The second and third loops compare the second and third string pairs.

Results: The test indicates that the longer the strings being compared, the longer the comparison takes.

However: The results are not tied to the string Length, but the number of characters at the start that are different.

Discussion. After running the benchmark, I modified it so that a string in each pair has an earlier character different in the second string. The results showed that the execution times decreased.

So: The conclusion is that the string equality performance is linked to the first different char.

And: The internal loop for the comparison starts at index 1 and moves forward. The time depends on the first different char position.

Summary. String performance depends on character data. We can modify the contents to enhance performance of Equals. The number of characters at the start of each string that are equal influence comparison time.

Finally: The impact of string equality comparisons on many programs' performance is low.