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条回答
梦醉为红颜
3楼-- · 2018-12-31 08:41

My approach has always been to use StringBuilder when concatenating 4 or more strings OR When I don't know how may concatenations are to take place.

Good performance related article on it here

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

I believe StringBuilder is faster if you have more than 4 strings you need to append together. Plus it can do some cool things like AppendLine.

查看更多
深知你不懂我心
5楼-- · 2018-12-31 08:43

StringBuilder is significantly more efficient but you will not see that performance unless you are doing a large amount of string modification.

Below is a quick chunk of code to give an example of the performance. As you can see you really only start to see a major performance increase when you get into large iterations.

As you can see the 200,000 iterations took 22 seconds while the 1 million iterations using the StringBuilder was almost instant.

string s = string.Empty;
StringBuilder sb = new StringBuilder();

Console.WriteLine("Beginning String + at " + DateTime.Now.ToString());

for (int i = 0; i <= 50000; i++)
{
    s = s + 'A';
}

Console.WriteLine("Finished String + at " + DateTime.Now.ToString());
Console.WriteLine();

Console.WriteLine("Beginning String + at " + DateTime.Now.ToString());

for (int i = 0; i <= 200000; i++)
{
    s = s + 'A';
}

Console.WriteLine("Finished String + at " + DateTime.Now.ToString());
Console.WriteLine();
Console.WriteLine("Beginning Sb append at " + DateTime.Now.ToString());

for (int i = 0; i <= 1000000; i++)
{
    sb.Append("A");
}
Console.WriteLine("Finished Sb append at " + DateTime.Now.ToString());

Console.ReadLine();

Result of the above code:

Beginning String + at 28/01/2013 16:55:40.

Finished String + at 28/01/2013 16:55:40.

Beginning String + at 28/01/2013 16:55:40.

Finished String + at 28/01/2013 16:56:02.

Beginning Sb append at 28/01/2013 16:56:02.

Finished Sb append at 28/01/2013 16:56:02.

查看更多
谁念西风独自凉
6楼-- · 2018-12-31 08:44

To clarify what Gillian said about 4 string, if you have something like this:

string a,b,c,d;
 a = b + c + d;

then it would be faster using strings and the plus operator. This is because (like Java, as Eric points out), it internally uses StringBuilder automatically (Actually, it uses a primitive that StringBuilder also uses)

However, if what you are doing is closer to:

string a,b,c,d;
 a = a + b;
 a = a + c;
 a = a + d;

Then you need to explicitly use a StringBuilder. .Net doesn't automatically create a StringBuilder here, because it would be pointless. At the end of each line, "a" has to be an (immutable) string, so it would have to create and dispose a StringBuilder on each line. For speed, you'd need to use the same StringBuilder until you're done building:

string a,b,c,d;
StringBuilder e = new StringBuilder();
 e.Append(b);
 e.Append(c);
 e.Append(d);
 a = e.ToString();
查看更多
春风洒进眼中
7楼-- · 2018-12-31 08:44

StringBuilder is preferable IF you are doing multiple loops, or forks in your code pass... however, for PURE performance, if you can get away with a SINGLE string declaration, then that is much more performant.

For example:

string myString = "Some stuff" + var1 + " more stuff"
                  + var2 + " other stuff" .... etc... etc...;

is more performant 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..

In this case, StringBuild could be considered more maintainable, but is not more performant than the single string declaration.

9 times out of 10 though... use the string builder.

On a side note: string + var is also more performant that the string.Format approach (generally) that uses a StringBuilder internally (when in doubt... check reflector!)

查看更多
登录 后发表回答