Performance Tips: Faster than StringBuilder?

The web has tons of articles about how StringBuilder is much faster than string concatenation using ‘+’ operator or String.Concat functions when there are enough strings to ber merged. Here is an MSDN article on this topic:

But actually, if all you want is concatenating strings, there is a more efficient way of doing so, the String.Join method. String.Join is more efficient in both memory and CPU. Here is a test program:

 const int sLen = 30, Loops = 5000;

System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();

string sSource = new String('X', sLen);
string sDest = "";

// Time string concatenation.
for (int i = 0; i < Loops; i++) sDest += sSource;
Console.WriteLine("Concatenation took {0,8:N3} ms. {1} chars", watch.Elapsed.TotalMilliseconds, sDest.Length);

// Time StringBuilder.
System.Text.StringBuilder sb = new System.Text.StringBuilder(sLen * Loops);
for (int i = 0; i < Loops; i++) sb.Append(sSource);
sDest = sb.ToString();
Console.WriteLine("StringBuilder took {0,8:N3} ms. {1} chars", watch.Elapsed.TotalMilliseconds, sDest.Length);

// Time String.Join.
string[] list = new string[Loops];
for (int i = 0; i < Loops; i++) list[i] = sSource;
sDest = String.Join(String.Empty, list, 0, Loops);
Console.WriteLine("String.Join took {0,8:N3} ms. {1} chars", watch.Elapsed.TotalMilliseconds, sDest.Length);


 Concatenation took 524.630 ms. 150000 chars
StringBuilder took 0.426 ms. 150000 chars
String.Join took 0.310 ms. 150000 chars

StringBuilder.Append takes 0.426 ms, copying string data twice. String.Join takes 0.310 ms, 27% less, copying string data only once.

StringBuilder uses 300 kb (30 * 5000 * 2) extra memory (in StringBuilder object), String.Join only uses 20 kb (5000 * pointer size) extra memory (in list).

So, if you only need to merge full strings, give String.Join a try. But StringBuilder is much much more powerful than just merging full strings.

Comments (7)

  1. hm, well

    Join() uses StringBuilder.

  2. @udger It depends what you're joining, right? Join(string[]) is quite efficient, Join(IEnumerable<string>) just uses stringbuilder

  3. Eddie Garmon says:

    Yep, this has been the case for a while.…/351707.aspx

  4. Feng Yuan says:

    Yeah, source code .Net is just released to public. You can read String.cs. Join using string array does not use StringBuilder.

  5. Salman Farsi says:

    Thanks for good informative piece of code, really very helpful when we are dealing with longer strings.


  6. Corneliu says:

    I've noticed on my PC that if I set the  Loops = 50 StringBuilder is faster than string.Join. (Not 51 or 49, only when is 50).

  7. Jifeng Hu says:

    Just now am focus on WINDOW platform, know you for your famous book of

    Windows Graphic Programming .

    welcome to

    Best regards!

Skip to main content