Welcome to MSDN Blogs Sign in | Join | Help

String Concatenation vs. StringBuilder.Append

I had a question the other day in an interview about the performance of StringBuilder and while I had run performance tests with StringBuilder in the past I had never really looked under the covers to determine why StringBuilder was or wasn't faster then string concatenation. So now I am going to ask you given the following code which function on the average is faster and why?

public static string StringBuilderConcat(string a, string b)
{
      
StringBuilder x = new StringBuilder();
       x.Append(a);
       x.Append(b);
       return x.ToString();
}

public static string StringConcat(string a, string b)
{
      
string x = a + b;
      
return x;
}

Now what if we introduce this function?

public static string StringBuilderConcat2(string a, string b)
{
       StringBuilder x =
new StringBuilder(a.Length + b.Length);
       x.Append(a);
       x.Append(b);
       return x.ToString();
}

--Eric (Grand Valley State University)

Published Monday, January 31, 2005 6:37 PM by kevinbri
Filed under:

Comments

# re: String Concatenation vs. StringBuilder.Append

Monday, January 31, 2005 9:31 PM by Kevin Marquette
I saw your post and had to try it out.

The results suprised me at first. but after thinging about the cost of creating a string builder every time I want to concat 2 strings, the results fit better.

Althought when appending a large number of string, the string builder wins out.

# re: String Concatenation vs. StringBuilder.Append

Monday, January 31, 2005 10:11 PM by S N
StringBuilder x = new StringBuilder(a.Length + b.Length);


should be changed to StringBuilder x = new StringBuilder(a.Length + b.Length + 1);

to avoid resizing of the internal buffer. The plus 1 is for null terminator.Otherwise the internal buffer would be resized to 2 * (a.Length + b.Length).

Also if you look at the StringBuilder.ToString(), if the internal capacity is less than half of the string length, then it will return the internal buffer as a string object. You don't want to carry a string that wastes too much of unused space (considering each char takes 2 bytes).

# re: String Concatenation vs. StringBuilder.Append

Monday, January 31, 2005 10:20 PM by Brant Gurganus
My response comes from the Java world, but these should all be roughly the same. Concatenating with a '+' implicitly creates a StringBuilder, basically it is the same as the function given in your example. The performance of a StringBuilder over raw concatenation doesn't become that noticable until you do around five or more concatenations consecutively.

# re: String Concatenation vs. StringBuilder.Append

Monday, January 31, 2005 11:44 PM by Wesner Moise
As written, StringConcat will always be faster than the other two functions.

Brant is incorrect; concatenating with a "+" is a direct concatenation and does not use StringBuilder at all.

# re: String Concatenation vs. StringBuilder.Append

Tuesday, February 01, 2005 6:38 AM by Eric Maino
Great comments so far... Though there are still some questions unanswered...

Any other thoughts?

# re: String Concatenation vs. StringBuilder.Append

Tuesday, February 01, 2005 7:40 AM by Jeff Parker
Are you looking for the answer Eric? Or you quizing the minds?

# re: String Concatenation vs. StringBuilder.Append

Tuesday, February 01, 2005 9:20 AM by Eric Maino
I have the answer. So I guess I am quizing the minds. I will post the answer in a few days.

# re: String Concatenation vs. StringBuilder.Append

Thursday, February 03, 2005 7:37 AM by Shane
The + method is going to be faster. I've heard a few times that you don't want to use the string builder unless you are doing at least 10+ concatenations. Not sure if this is the exact number but it does make sense that the time required to build the object itself adds some overhead to the string builder method.

# re: String Concatenation vs. StringBuilder.Append

Thursday, February 03, 2005 7:44 PM by Brendan
I’m going to have to agree with Shane, I will explain a bit more though…

While StringBuilder is useful, if it were the end all be all solution for string concatenation, it would not exist. Why? Because it’s ultra efficient concatenation methods would exist natively within the regular String class. There would be no need for a secondary way to concat strings.
New Comments to this post are disabled
 
Page view tracker