I understand the difference between String
and StringBuilder
(StringBuilder
being mutable) but is there a large performance difference between the two?
The program I’m working on has a lot of case driven string appends (500+). Is using StringBuilder
a better choice?
Using strings for concatenation can lead to a runtime complexity on the order of
O(n^2)
.If you use a
StringBuilder
, there is a lot less copying of memory that has to be done. With theStringBuilder(int capacity)
you can increase performance if you can estimate how large the finalString
is going to be. Even if you're not precise, you'll probably only have to grow the capacity ofStringBuilder
a couple of times which can help performance also.This benchmark shows that regular concatenation is faster when combining 3 or fewer strings.
http://www.chinhdo.com/20070224/stringbuilder-is-not-always-faster/
StringBuilder can make a very significant improvement in memory usage, especially in your case of adding 500 strings together.
Consider the following example:
What happens in memory? The following strings are created:
By adding those five numbers to the end of the string we created 13 string objects! And 12 of them were useless! Wow!
StringBuilder fixes this problem. It is not a "mutable string" as we often hear (all strings in .NET are immutable). It works by keeping an internal buffer, an array of char. Calling Append() or AppendLine() adds the string to the empty space at the end of the char array; if the array is too small, it creates a new, larger array, and copies the buffer there. So in the example above, StringBuilder might only need a single array to contain all 5 additions to the string-- depending on the size of its buffer. You can tell StringBuilder how big its buffer should be in the constructor.
I have seen significant performance gains from using the
EnsureCapacity(int capacity)
method call on an instance ofStringBuilder
before using it for any string storage. I usually call that on the line of code after instantiation. It has the same effect as if you instantiate theStringBuilder
like this:This call allocates needed memory ahead of time, which causes fewer memory allocations during multiple
Append()
operations. You have to make an educated guess on how much memory you will need, but for most applications this should not be too difficult. I usually err on the side of a little too much memory (we are talking 1k or so).String concatenation will cost you more. In Java, You can use either StringBuffer or StringBuilder based on your need. If you want a synchronized, and thread safe implementation, go for StringBuffer. This will be faster than the String concatenation.
If you do not need synchronized or Thread safe implementation, go for StringBuilder. This will be faster than String concatenation and also faster than StringBuffer as their is no synchorization overhead.
As a general rule of thumb, if I have to set the value of the string more than once, or if there are any appends to the string, then it needs to be a string builder. I have seen applications that I have written in the past before learning about string builders that have had a huge memory foot print that just seems to keep growing and growing. Changing these programs to use the string builder cut down the memory usage significantly. Now I swear by the string builder.
StringBuilder will perform better, from a memory stand point. As for processing, the difference in time of execution may be negligible.