Partager via


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)

Comments

  • Anonymous
    January 31, 2005
    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.
  • Anonymous
    January 31, 2005
    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).

  • Anonymous
    January 31, 2005
    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.
  • Anonymous
    January 31, 2005
    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.
  • Anonymous
    January 31, 2005
    Great comments so far... Though there are still some questions unanswered...

    Any other thoughts?
  • Anonymous
    January 31, 2005
    Are you looking for the answer Eric? Or you quizing the minds?
  • Anonymous
    February 01, 2005
    I have the answer. So I guess I am quizing the minds. I will post the answer in a few days.
  • Anonymous
    February 02, 2005
    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.
  • Anonymous
    February 03, 2005
    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.