I was wondering about StringBuilder and I’ve got a question that I was hoping the community would be able to explain.
Let’s just forget about code readability, which of these is faster and why?
StringBuilder.Append:
StringBuilder sb = new StringBuilder(); sb.Append(string1); sb.Append('----'); sb.Append(string2);
StringBuilder.AppendFormat:
StringBuilder sb = new StringBuilder(); sb.AppendFormat('{0}----{1}',string1,string2);
It’s impossible to say, not knowing the size of
string1andstring2.With the call to
AppendFormat, it will preallocate the buffer just once given the length of the format string and the strings that will be inserted and then concatenate everything and insert it into the buffer. For very large strings, this will be advantageous over separate calls toAppendwhich might cause the buffer to expand multiple times.However, the three calls to
Appendmight or might not trigger growth of the buffer and that check is performed each call. If the strings are small enough and no buffer expansion is triggered, then it will be faster than the call toAppendFormatbecause it won’t have to parse the format string to figure out where to do the replacements.More data is needed for a definitive answer
It should be noted that there is little discussion of using the static
Concatmethod on theStringclass (Jon’s answer usingAppendWithCapacityreminded me of this). His test results show that to be the best case (assuming you don’t have to take advantage of specific format specifier).String.Concatdoes the same thing in that it will predetermine the length of the strings to concatenate and preallocate the buffer (with slightly more overhead due to looping constructs through the parameters). It’s performance is going to be comparable to Jon’sAppendWithCapacitymethod.Or, just the plain addition operator, since it compiles to a call to
String.Concatanyways, with the caveat that all of the additions are in the same expression:NOT
For all those putting up test code
You need to run your test cases in separate runs (or at the least, perform a GC between the measuring of separate test runs). The reason for this is that if you do say, 1,000,000 runs, creating a new
StringBuilderin each iteration of the loop for one test, and then you run the next test that loops the same number of times, creating an additional 1,000,000StringBuilderinstances, the GC will more than likely step in during the second test and hinder its timing.