StringBuilder is not always faster – Part 2 of 2

In a previous article (“StringBuilder is not always faster), I provided some quick benchmark data and gave “rules of thumb” for when to use StringBuilder and when to use traditional string concatenations. In this follow-up article, I will attempt to provide a more detailed analysis.

If you don’t want to bother with the details, jump directly to the conclusions here.

A Look at the Generated MSIL

A reader, Matt, suggested that it’s possible the compiler may have noticed that I never use the generated test objects in my benchmark code and not created them. That would definitely invalidated my test results!

Size of Concatenated Values and StringBuilder with Initial Capacity

More questions from Matt: how does the performance curve change if the concatenated values are larger? And what if you seed the StringBuilder object with an initial capacity?

To answer the questions, I wrote some more benchmarks (complete source code at the bottom of this article) to compare three different methods of concatenation:

“+” concatenation: This is the traditional a + b + c method.

StringBuilder: Create a StringBuilder object using the default constructor and calling Append().

StringBuilder/Pre-allocated: Create a StringBuilder object and preallocate the initial capacity so that it does not need to expand later.

String Size = 10

The chart below shows elapsed times (ms) of the three concatenation methods, with the concatenated string size equaled to 10 characters

As the chart illustrates, when the size of the concatenated value is small (10 characters in this test), “+” concatenation (blue line) performs faster than StringBuilder until the number of concatenations reaches 6. After 6, StringBuilder starts to work exponentially faster.

However, when compared with the StringBuilder/Pre-allocated method, StringBuilder starts to perform as fast as “+” concatenation much earlier: at 3 concatenations.

Note: The orange line for StringBuilder is not very linear. My guess is that it’s due to the need to allocate space as needed. The memory allocation itself will consume CPU cycles. The default StringBuilder constructor will allowcate 16 bytes initially. Thereafter, it will allocate two times the current capacity whenever needed.

String Size = 100

When the concatenated value is 100 characters, the all three methods perform very similarly up to three concatenations, then StringBuilder/Pre-allocated pulls ahead at 4 concatenations.

Concatenated String Size = 1000

At 1000 characters, things begine a little bit more interesting: StringBuilder/Pre-allocated is faster in all cases (although the difference is very small until about 6 concatenations). Since it may not be always possible or practical to know the final string size ahead of time, for this graph, I also added two more series to show what happens if you over-estimate or under-estimate the final capacity. As expected, there is a performance penalty for both. The more inaccurate your estimated capacity is, the higher of a performance penalty you will get.

What about String.Concat?

Lars Wilhelmsen asked “What about string.Concat?” According to my research, string.Concat is basically identical to “+” used on a single line.

This:

string s = "a" + class="str">"b" + "c";

Is the same (same generated IL) as this:

string s = string.Concat( class="str">"a", "b", "c");

But not this:

string s = "a";
s = s + "b";
s = s + "c";

Remember, the “+” must be on the same logical line, otherwise, the compiler will convert each line into a separate string.Concat operation, resulting in slower performance.

AJ has written a post detailing string.Concat here. Thanks, AJ, for pointing it out.

And String.Format?

Flyswat wanted to know about string.Format. I did some quick benchmark code again. The string.Format code below took 58 milliseconds to run 100,000 iterations:

string s = string.Format( class="str">"Value: {0}", strVal);

While this code, using “+” concatenation, only took 9 milliseconds (also 100,000 iterations):

string s = "Value: " + strVal;

According to the above numbers, string.Format is significantly slower than “+” concatenations. The difference in speed is similar between s.AppendFormat(“Value: {0}”, strVal) and s.Append(“Value: ” + strVal). I have used String.Format a lot in my code and I have not thought about this performance penalty. It does make sense. String.Format (or StringBuilder.AppendFormat) has to scan the string looking for format specifiers… that takes time. String.Format is very useful to make the code easier to read or when you actually need to format numbers. Even with this new data, I will not neccessarily shy away from using string.Format. I will however definitely be much more observant when using it, especially when used inside a loop or performance critical code path.

Conclusions

The new benchmarks do point to StringBuilder/Pre-allocated as the fastest method regardless of number of concatenations, when the concatenated string value is large (1000 characters).

With that in mind, here are my slightly modified rules of thumbs for string concatenation. Remember, “rules of thumb” are short statements to provide general princicles, and may not be accurate for every single situation. For performance criticial code, you should consider running some benchmarks/profiling yourself.

For 1-4 dynamic concatenations, use traditional “+” concatenation.

For 5 or more dynamic concatenations, use StringBuilder.

When using StringBuilder, try to provide the starting capacity as close to the final string size as possible.

When building a big string from several string literals, use either the @ string literal or the + operator.

Well,
StringBuilder with specified capacity is always faster. You measured only first phase of concatenation – memory allocation. Due to fact that strings are immutable each time we are using “+” operator or String.Concat() the new String object will be allocated in memory which in your case you will have 100000 String objects in memory where 99999 are not used at all.

So as you can guess we are creating additional work for a Garbage Collector and using more memory than it actually could be used.

In case of StringBuilder with specified capacity there will be only 1 object created in memory and *NO* additional work for Garbage Collector.

“+” operator takes more CPU time, Memory size and creates extra time for a Garbage Collector to clear the mess it built. Unlike StringBuilder with specified parameter saves memory space and CPU time as object will be allocated only ones and GarbageCollector won’t have anything to clean up after.

My Conclusion is: Use StringBuilder with specified capacity almost always.

I’ll have to disagree. The tests I did in this article precisely showed that under some circumstances, + concatenation is faster than StringBuilder, even when pre-allocated with the final capacity. Look at the first test where the concatenated string size is 10. + concatenations is faster up to 3 concatenations.

I understand that the more memory you allocate, the more work you create for the garbage collector. However, the work that is done by the garbage collector is also taken into account in my tests, as I simply measured the total elapsed time. Whatever work that is done by the GC is reflected in that total elapsed time.

Re the “for” loop, its purpose is to purely multiply the concatenation multiple times to get a more accurate measurement. It’s a typical practice in micro-benchmarks. The operation being measured is inside the loop.

Tests you did are really good and very detail. They proof that nothing is so obvious and simple. It is nice to see that there is another person in this world who thinks about same kind of crazy things

I would like to leave for developers the choice they will make whether to use different techniques based on number of concatenations + length of strings or just to use StringBuilder(capacity) everywhere.

In code, you don’t instantiate a million new StringBuilders. StringBuilders are a little expensive up front and the fact that you’re instantiating it inside the loop is where the time is lost.

Instantiation is expensive. That’s why the stringbuilder is so much better than traditional “+” concatenation that spans multiple calls.

In a coding project, you only create the StringBuilder once then you can use it to continually, efficiently concatenate your variable string, as needed. Please try pulling that line (“StringBuilder sb = new StringBuilder();”) out and check out the results… something like this:

CoDr: Thanks for the comment. I think you may have misunderstood the purpose of my “for loop”. The loop itself is not what is being tested… think of it as “testing framework” code. The code I am testing is inside the loop. In these kinds of benchmarks, where things can happen so fast, these loops are used to perform “the test” repeatedly so that we can get a better average result in the end.

If you don’t have the for loop, results from one test to the next are likely to vary significantly. That is if your timer is even accurate enough to measure the few microseconds it would take to concatenate those strings.

In my own testing, I noticed that placing all the concatenations on one line vastly affected performance. For my tests, I had each operation run x number of time inside a loop. I then had *that* execute x number of times inside another loop. The inner loop (function) did the actual time clocking, while the outer loop was mainly used to execute the specific benchmark x times, and set performance numbers for reporting.

For a x value of 1000 (1000 function calls, with each function call looping 1000 times) I came up with the conclusion that the + operator (actually, I used “&” in vb.net) was faster than the StringBuilder method. This was with the both functions concatenation all on the same line. When I moved the concatenation to separate lines StringBuilder came out on top. Not by much mind you.

I concatenated the string “Test: ” followed by the loop counter variable (integer) nine times, calling the ToString method on it.