C# 字符串与 StringBuilder
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/73883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String vs. StringBuilder
提问by Kuvo
I understand the difference between String
and StringBuilder
(StringBuilder
being mutable) but is there a large performance difference between the two?
我了解String
和StringBuilder
(StringBuilder
可变)之间的区别,但是两者之间的性能差异很大吗?
The program I'm working on has a lot of case driven string appends (500+). Is using StringBuilder
a better choice?
我正在开发的程序有很多大小写驱动的字符串附加(500+)。使用StringBuilder
是更好的选择吗?
采纳答案by Jay Bazuzi
Yes, the performance difference is significant. See the KB article "How to improve string concatenation performance in Visual C#".
是的,性能差异是显着的。请参阅知识库文章“如何提高 Visual C# 中的字符串连接性能”。
I have always tried to code for clarity first, and then optimize for performance later. That's much easier than doing it the other way around! However, having seen the enormous performance difference in my applications between the two, I now think about it a little more carefully.
我总是尝试先编码以提高清晰度,然后再优化性能。这比反过来做要容易得多!但是,在看到我的应用程序在两者之间的巨大性能差异之后,我现在更仔细地考虑了一下。
Luckily, it's relatively straightforward to run performance analysis on your code to see where you're spending the time, and then to modify it to use StringBuilder
where needed.
幸运的是,对您的代码运行性能分析以查看您将时间花在哪里,然后修改它以StringBuilder
在需要的地方使用相对简单。
回答by DevelopingChris
StringBuilder will perform better, from a memory stand point. As for processing, the difference in time of execution may be negligible.
从内存的角度来看,StringBuilder 的性能会更好。至于处理,执行时间的差异可以忽略不计。
回答by Gilligan
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.
如果您需要将超过 4 个字符串附加在一起,我相信 StringBuilder 会更快。另外它可以做一些很酷的事情,比如 AppendLine。
回答by Eric Z Beard
In .NET, StringBuilder is still faster than appending strings. I'm pretty sure that in Java, they just create a StringBuffer under the hood when you append strings, so there's isn't really a difference. I'm not sure why they haven't done this in .NET yet.
在 .NET 中,StringBuilder 仍然比附加字符串更快。我很确定在 Java 中,当您附加字符串时,它们只是在后台创建了一个 StringBuffer,因此实际上并没有什么区别。我不确定为什么他们还没有在 .NET 中这样做。
回答by deemer
StringBuilder is probably preferable. The reason is that it allocates more space than currently needed (you set the number of characters) to leave room for future appends. Then those future appends that fit in the current buffer don't require any memory allocation or garbage collection, which can be expensive. In general, I use StringBuilder for complex string concatentation or multiple formatting, then convert to a normal String when the data is complete, and I want an immutable object again.
StringBuilder 可能更可取。原因是它分配的空间比当前需要的多(您设置字符数),以便为将来的追加留出空间。然后那些适合当前缓冲区的未来追加不需要任何内存分配或垃圾收集,这可能很昂贵。一般情况下,我使用StringBuilder进行复杂的字符串连接或多重格式化,然后在数据完成后转换为普通String,我又想要一个不可变对象。
回答by Alex Fort
If you're doing a lot of string concatenation, use a StringBuilder. When you concatenate with a String, you create a new String each time, using up more memory.
如果您要进行大量字符串连接,请使用 StringBuilder。与 String 连接时,每次都会创建一个新 String,从而占用更多内存。
Alex
亚历克斯
回答by RichS
Further to the previous answers, the first thing I always do when thinking of issues like this is to create a small test application. Inside this app, perform some timing test for both scenarios and see for yourself which is quicker.
除了前面的答案之外,在考虑此类问题时,我总是做的第一件事就是创建一个小型测试应用程序。在这个应用程序中,对这两种情况进行一些计时测试,然后自己看看哪个更快。
IMHO, appending 500+ string entries should definitely use StringBuilder.
恕我直言,附加 500 多个字符串条目绝对应该使用 StringBuilder。
回答by Steve g
Using strings for concatenation can lead to a runtime complexity on the order of O(n^2)
.
使用字符串进行连接可能会导致运行时复杂度为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.
如果您使用 a StringBuilder
,则必须完成的内存复制要少得多。有了StringBuilder(int capacity)
你可以提高性能,如果你能估计最终有多大String
将是。即使您不精确,您也可能只需要将容量增加StringBuilder
几次,这也有助于提高性能。
回答by moswald
StringBuilder reduces the number of allocations and assignments, at a cost of extra memory used. Used properly, it can completely remove the need for the compiler to allocate larger and larger strings over and over until the result is found.
StringBuilder 以使用额外内存为代价减少了分配和分配的数量。使用得当,它可以完全消除编译器一遍遍分配越来越大的字符串直到找到结果的需要。
string result = "";
for(int i = 0; i != N; ++i)
{
result = result + i.ToString(); // allocates a new string, then assigns it to result, which gets repeated N times
}
vs.
对比
String result;
StringBuilder sb = new StringBuilder(10000); // create a buffer of 10k
for(int i = 0; i != N; ++i)
{
sb.Append(i.ToString()); // fill the buffer, resizing if it overflows the buffer
}
result = sb.ToString(); // assigns once
回答by moswald
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.
我的方法一直是在连接 4 个或更多字符串时使用 StringBuilder,或者当我不知道如何进行连接时。