String vs. StringBuilder

2018-12-31 08:24发布

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?

25条回答
余生请多指教
2楼-- · 2018-12-31 08:25

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 the StringBuilder(int capacity) you can increase performance if you can estimate how large the final String is going to be. Even if you're not precise, you'll probably only have to grow the capacity of StringBuilder a couple of times which can help performance also.

查看更多
墨雨无痕
3楼-- · 2018-12-31 08:26

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:

string buffer = "The numbers are: ";
for( int i = 0; i < 5; i++)
{
    buffer += i.ToString();
}
return buffer;

What happens in memory? The following strings are created:

1 - "The numbers are: "
2 - "0"
3 - "The numbers are: 0"
4 - "1"
5 - "The numbers are: 01"
6 - "2"
7 - "The numbers are: 012"
8 - "3"
9 - "The numbers are: 0123"
10 - "4"
11 - "The numbers are: 01234"
12 - "5"
13 - "The numbers are: 012345"

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.

查看更多
残风、尘缘若梦
4楼-- · 2018-12-31 08:26

I have seen significant performance gains from using the EnsureCapacity(int capacity) method call on an instance of StringBuilder 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 the StringBuilder like this:

var sb = new StringBuilder(int capacity);

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).

查看更多
呛了眼睛熬了心
5楼-- · 2018-12-31 08:29

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.

查看更多
君临天下
6楼-- · 2018-12-31 08:29

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.

查看更多
泪湿衣
7楼-- · 2018-12-31 08:29

StringBuilder will perform better, from a memory stand point. As for processing, the difference in time of execution may be negligible.

查看更多
登录 后发表回答