Problem
I understand the distinction between String and StringBuilder (StringBuilder being mutable), but is there a significant performance difference?
There are over 500 case-driven string appends in the software I’m working on. Is it better to use StringBuilder?
Asked by Kuvo
Solution #1
Yes, there is a considerable difference in performance. See “How to Improve String Concatenation Performance in Visual C#” in the Knowledge Base.
I’ve always tried to code for readability first, then optimize for speed later. It’s a lot easier this way than it is the other way around! However, now that I’ve observed the huge performance disparity between the two in my applications, I’m thinking about it a little more seriously.
Fortunately, performing a performance study on your code to discover where you’re wasting time is pretty simple, and then modifying it to utilize StringBuilder when necessary.
Answered by Jay Bazuzi
Solution #2
If you have something like this, you can clarify what Gillian mentioned about 4 string:
string a,b,c,d;
a = b + c + d;
Then strings and the plus operator would be more efficient. This is due to the fact that it (like Java, as Eric points out) utilizes StringBuilder internally (Actually, it uses a primitive that StringBuilder also uses)
If, on the other hand, what you’re doing is more akin to:
string a,b,c,d;
a = a + b;
a = a + c;
a = a + d;
Then you must use a StringBuilder explicitly. Because it would be unnecessary, Net does not immediately generate a StringBuilder here. Because “a” must be a (immutable) string at the end of each line, it must generate and dispose of a StringBuilder on each line. You’d have to keep using the same StringBuilder until you’re done creating for speed:
string a,b,c,d;
StringBuilder e = new StringBuilder();
e.Append(b);
e.Append(c);
e.Append(d);
a = e.ToString();
Answered by James Curran
Solution #3
If you’re doing several loops or forks in your code, StringBuilder is preferred… but for PURE efficiency, if you can get away with a SINGLE string declaration, that’s far better.
For example:
string myString = "Some stuff" + var1 + " more stuff"
+ var2 + " other stuff" .... etc... etc...;
is more effective than
StringBuilder sb = new StringBuilder();
sb.Append("Some Stuff");
sb.Append(var1);
sb.Append(" more stuff");
sb.Append(var2);
sb.Append("other stuff");
// etc.. etc.. etc..
StringBuild is more maintainable in this example, although it is not more performant than a single string declaration.
However, utilize the string builder 9 times out of 10.
Aside from that, string + var is also faster than the string. When in doubt, check the reflector!) for a format technique that uses a StringBuilder inside.
Answered by calebjenkins
Solution #4
A basic illustration of the speed difference between String concatenation and StringBuilder is as follows:
System.Diagnostics.Stopwatch time = new Stopwatch();
string test = string.Empty;
time.Start();
for (int i = 0; i < 100000; i++)
{
test += i;
}
time.Stop();
System.Console.WriteLine("Using String concatenation: " + time.ElapsedMilliseconds + " milliseconds");
Result:
StringBuilder test1 = new StringBuilder();
time.Reset();
time.Start();
for (int i = 0; i < 100000; i++)
{
test1.Append(i);
}
time.Stop();
System.Console.WriteLine("Using StringBuilder: " + time.ElapsedMilliseconds + " milliseconds");
Result:
As a consequence, the first iteration took 15423 milliseconds, while the second iteration took 10 milliseconds using StringBuilder.
Using StringBuilder appears to be significantly faster.
Answered by Diizzy
Solution #5
When combining three or less strings, this benchmark reveals that conventional concatenation is faster.
StringBuilder is not always faster – Part 1 of 2
StringBuilder can make a very significant improvement in memory usage, especially in your case of adding 500 strings together.
Consider the following illustration:
string buffer = "The numbers are: ";
for( int i = 0; i < 5; i++)
{
buffer += i.ToString();
}
return buffer;
What occurs when you remember something? Strings are formed as follows:
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"
We made 13 string objects by adding those five integers to the end of the string! And 12 of them were completely ineffective! Wow!
This issue is resolved by using StringBuilder. It’s not a “mutable string,” as we’re used to hearing (all strings in .NET are immutable). It functions by maintaining an internal buffer, which is an array of chars. Append() or AppendLine() appends the string to the char array’s empty space; if the array is too small, it generates a new, larger array and copies the buffer there. So, depending on the size of its buffer, StringBuilder might only need a single array to hold all 5 additions to the string in the example above. In the constructor, you can tell StringBuilder how big its buffer should be.
Answered by Matt Trunnell
Post is based on https://stackoverflow.com/questions/73883/string-vs-stringbuilder