Compare the speeds of different methods for string concatenation in C#

example

There are several ways you can perform string concatenation in C#. 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 text “1234567890” to the current string and the number of trials you want to perform. 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 class any time they need to concatenate strings, but the table shows that StringBuilder takes longer when you’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");

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 +=.

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

Personally, I find this version the easiest to read, so it's what I generally use.


Download Example   Follow me on Twitter   RSS feed




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

One Response to Compare the speeds of different methods for string concatenation in C#

  1. Johnny Boy says:

    Here’s a blog which benchmarks numerous ways to compare strings in C#:

    http://cc.davelozinski.com/c-sharp/fastest-way-to-compare-strings

    _

Leave a Reply

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