String Performance: Concatenating Strings

There are multiple ways to concatenate strings together in Microsoft .NET. I would say most developers would do it like this.

string result = "David" + "McCarter"; 

Before we talk about the rest of this section, I must stop and remind you that strings in .NET are immutable. Because of this, combining multiple strings together could cause a performance and memory issue. I see developers doing this in their code often, even for building large SQL queries (which could also be a security issue).

To benchmark the performance, I am looping through a string array to concatenate the strings. Here is the code that was used for the report.

var result = string.Empty;

Example #1

string.Concat(this.stringArray);

Example #2

foreach (var item in this._stringArray)
{
   result = string.Concat(result, item, ControlChars.Space);
}

Example #3

foreach (var item in this._stringArray)
{
   result = string.Join(ControlChars.Space, item);
}

Example #4

foreach (string _stringArray in this._stringArray)
{
   result = result + _stringArray + ControlChars.Space;
}

or

foreach (string _stringArray in this._stringArray)
{
   result += _stringArray + ControlChars.Space;
}

The drawback to using Concat() is that adding a separator cannot be used.

Benchmark Results

Let’s look at the performance of these four methods of combining strings from a string array with different word counts.

COMBINING STRINGS-CHART

As you can see, using Concat() is the fastest followed by Join(). Using + or += is close to the same performance except for the array with 250 words to combine.

Allocations

There is a huge difference in the allocations for these three benchmark tests.

  • Concat() & Join(): 88 to 7,528 bytes
  • + and +=: 88 to 947,448 bytes

My recommendation is to use Concat() since it is more performant if you don’t need to separate the strings with a character. If you do, then use string.Join().

I’ve been warning software engineers for a very long time about the performance and memory issues when using += !

You can also use the StringBuilder to improve performance!

String Performance: Combining Strings with the StringBuilder

If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.

2 thoughts on “String Performance: Concatenating Strings

  1. Curious that you did not benchmark StringBuilder as it is usually the recommended approach for multiple appends/concatentations…..

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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