Jaa


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.

Comments

  • Anonymous
    August 14, 2012
    hm, well Join() uses StringBuilder.

  • Anonymous
    August 14, 2012
    @udger It depends what you're joining, right? Join(string[]) is quite efficient, Join(IEnumerable<string>) just uses stringbuilder

  • Anonymous
    August 14, 2012
    Yep, this has been the case for a while. weblogs.asp.net/.../351707.aspx

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

  • Anonymous
    August 15, 2012
    Thanks for good informative piece of code, really very helpful when we are dealing with longer strings. -Salman

  • Anonymous
    August 15, 2012
    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).

  • Anonymous
    September 24, 2012
    Just now am focus on WINDOW platform, know you for your famous book of Windows Graphic Programming . welcome to 2008jason.hu@gmail.com Best regards!