1. The document compares three common methods for concatenating strings in C#: plus concatenation, string.Format, and StringBuilder.
2. Through tests concatenating 1-100 single-character strings, it finds that StringBuilder is consistently the fastest and most memory efficient method, while plus concatenation becomes the slowest and most memory intensive for larger numbers of concatenations.
3. It concludes that StringBuilder should be used for performance-critical code, plus concatenation for simple cases, and string.Format is best for non-critical code involving larger numbers of concatenations, prioritizing readability over micro-optimizations.
2. Before I start
• These are the conclusions in the previous version of
the slides:
1. Use StringBuilder for performance critical code !
2. Never use plus concatenation !
3. For default usage, I personally recommend string.format
4. Prioritize code readability over performance
• Two were wrong, because of inaccurate tests.
– I’d like apologize to people who followed my previous
conclusions
4. What I want to do
• Minimize memory usage
– Done wrongly, it can consume a lot of
unnecessary memory (yes, that’s right)
• Maximize speed
• Get the most bang for the buck
5. There are three methods, AFAIK
• The usual plus concatenation:
– “foo” + “bar”
• string.Format:
– string.Format(“{0} {1}”, foo.ToString(),bar.ToString());
• StringBuilder
– StringBuilder string_builder = new StringBuilder("");
string_builder.append(foo.ToString());
string_builder.append(bar.ToString());
• Some articles on the Internet suggest using StringBuilder,
and some suggest using string.Format, but I haven’t
found any that tries to compare them.
– So, I did it.
6. How I did the comparison
• Prepare 100 one-letter strings.
• Perform 1..100th one-letter string concatenations for each
method. For example:
– “1” + “2” => 2 one-letter string concatenations
– “1” + “2” + “3” => 3 one-letter string concatenations
• Measure only the concatenation time for each method,
which means initialization codes should not be included.
– Do the concatenation 1000 times, so that we have meaningful
numbers to measure.
• Output the results in a text file, visualize in Excel
• Full source code can be found here:
https://github.com/sindharta/unity-sandbox
– Open the StringTest scene.
12. Analyzing a little bit more
• string.Format always slower than StringBuilder. This is
understandable because string.Format uses StringBuilder internally.
• string.Format pays initial cost at the beginning for trying to parse the
string format, which pays off when concatenating a lot of strings.
• The results of concatenating strings which have more than one
letters would be a lot different than the results shown here.
– Longer strings would slow down plus concatenations because it needs to
allocate more memory to accommodate these longer strings. The impact
would be much less for the other two methods.
– For string.Format to be able to win against plus concatenation in the case of
just two string concatenations, I would guess that the strings must have
more than 450 letters (1+2+3+…30), but more accurate test is needed.
• Code readability in order:
1. Plus concatenation
2. string.Format
3. StringBuilder
13. Conclusion
• Use StringBuilder for performance critical code.
• Use plus concatenation for simple cases.
• In the case non-critical but a lot of string
concatenations, use string.Format.
• When confused, prioritize code readability over
performance by default.