Compare the speeds of different methods for concatenating strings in C#

There are several ways you can concatenate strings. For example, you can use += to add text to an existing string, you can use string.Concat, and you can use the StringBuilder class. This example compares the performance of these methods.

Enter the number of times you want to add the string 1234567890 to the string and the number of trials you want to perform, and then click Go. The program performs the concatenation in the three methods and displays the result.

Here are some of the produced by my computer per trial in microseconds:

# Concatenations StringBuilder String.Concat +=
1 0.14 0.02 0.02
5 0.43 0.37 0.37
10 0.75 1.17 1.15
20 1.45 3.37 3.39
50 3.43 15.60 15.60
100 10.92 56.16 56.16

Many developers pull out the StringBuilder any time they need to concatenate strings but the table shows that StringBuilder takes longer when you0're only concatenating a few strings together. As the number of concatenations increases, StringBuilder becomes more useful.

However, remember that these times are in microseconds so even the slowest of these is quite fast. Concatenating 100 strings of 10 characters each takes only 0.00005616 seconds using the slowest methods. To look at it another way, you could perform that operation around 17,800 times per second.

Unless you need to perform a huge number of very large concatenations, I wouldn't worry about this tiny difference in speed. Instead take whatever approach makes your code easiest to read. Then you can revise it later if you discover there's a problem. In most programs, 20% of the code takes up 80% of the time (and in many cases %10 of the code takes up 90% of the time) so you'll usually do better in the long run making your code easy to understand and only optimizing the slowest 20% instead of wasting time and making the code harder to read by trying to optimize everything the first time through. I have rarely seen a project fail due to irreparable performance issues but I've often seen projects fail due to overly-complex and confusing code.

The following code shows how the program uses the StringBuilder class.

StringBuilder sb = new StringBuilder();
for (long i = 1; i <= num_appends; i++)
{
sb.Append("0123456789");
}
result = sb.ToString();

The following code shows how the program uses the string.Concat method.

result = "";
for (long i = 1; i <= num_appends; i++)
{
result = string.Concat(result, "0123456789");
}

The following code shows how the program uses +=. Personally I find this version the easiest to read so it's what I generally use.

result = "";
for (long i = 1; i <= num_appends; i++)
{
result += "0123456789";
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.