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: https://support.microsoft.com/kb/306822

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.
 // 
 watch.Restart();
 for (int i = 0; i < Loops; i++) sDest += sSource;
 watch.Stop();
 Console.WriteLine("Concatenation took {0,8:N3} ms. {1} chars", watch.Elapsed.TotalMilliseconds, sDest.Length);
 
 // Time StringBuilder.
 // 
 watch.Restart();
 System.Text.StringBuilder sb = new System.Text.StringBuilder(sLen * Loops);
 for (int i = 0; i < Loops; i++) sb.Append(sSource);
 sDest = sb.ToString();
 watch.Stop();
 Console.WriteLine("StringBuilder took {0,8:N3} ms. {1} chars", watch.Elapsed.TotalMilliseconds, sDest.Length);
 
 // Time String.Join.
 // 
 watch.Restart();
 string[] list = new string[Loops];
 for (int i = 0; i < Loops; i++) list[i] = sSource;
 sDest = String.Join(String.Empty, list, 0, Loops);
 watch.Stop();
 Console.WriteLine("String.Join took {0,8:N3} ms. {1} chars", watch.Elapsed.TotalMilliseconds, sDest.Length);
 

 Result:           

  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.