如何明智地使用 StringBuilder? [英] How to use StringBuilder wisely?
问题描述
我对使用 StringBuilder
类有点困惑,首先:
I am little confused about using StringBuilder
class, first:
string
对象连接操作总是从现有的 string
和新数据创建一个新对象.StringBuilder
对象维护一个缓冲区以容纳新数据的串联.如果空间可用,新数据将附加到缓冲区的末尾;否则,分配一个新的更大的缓冲区,将原始缓冲区中的数据复制到新缓冲区,然后将新数据附加到新缓冲区.
A
string
object concatenation operation always creates a new object from the existingstring
and the new data. AStringBuilder
object maintains a buffer to accommodate the concatenation of new data. New data is appended to the end of the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, then the new data is appended to the new buffer.
但是创建 StringBuilder
实例以避免创建新的 String
实例的重点在哪里?这听起来像是一对一"交易.
But where is the point of creating StringBuilder
instance to avoid creating new one of String
? It sounds like trading "one for one".
static void Main(string[] args)
{
String foo = "123";
using (StringBuilder sb = new StringBuilder(foo)) // also sb isn't disposable, so there will be error
{
sb.Append("456");
foo = sb.ToString();
}
Console.WriteLine(foo);
Console.ReadKey();
}
为什么我不应该只使用
+=
好的,我现在知道如何重用 StringBuilder
的一个实例(仍然不知道这对代码是否正确标准),但这不值得只用一个 string
,不是吗?
Ok, I know now how to reuse one instance of StringBuilder
(still don't know if this is right with code standards), but this isn't worth to use with just one string
, isn't it?
推荐答案
修改不可变 结构,如 string
s 必须通过复制结构来完成,这样会消耗更多内存并减慢应用程序的运行时间(也会增加 GC
时间等......).
Modifying immutable structures like string
s must be done by copying the structure, and by that, consuming more memory and slowing the application's run time (also increasing GC
time, etc...).
StringBuilder
通过使用相同的可变对象进行操作来解决这个问题.
StringBuilder
comes to solve this problem by using the same mutable object for manipulations.
但是:
在编译时连接 string
如下:
when concatenating a string
in compile time as the following:
string myString = "123";
myString += "234";
myString += "345";
它实际上会编译成这样:
it will actually compile to something like that:
string myString = string.Concat("123", "234", "345");
这个函数比使用 StringBuilder
更快,因为 string
进入函数的数量是已知的.
this function is faster than working with StringBuilder
for the number of string
s entering the function is known.
所以对于编译时已知的 string
连接,你应该更喜欢 string.Concat()
.
so for compile-time-known string
concatenations you should prefer string.Concat()
.
对于未知数量的string
,如下例:
as for unknown number of string
like in the following case:
string myString = "123";
if (Console.ReadLine() == "a")
{
myString += "234";
}
myString += "345";
现在编译器不能使用 string.Concat()
函数,但是,StringBuilder
似乎只有在串联时才在时间和内存消耗上更有效率用 6-7 个或更多 strings
完成.
Now the compiler can't use the string.Concat()
function, however, StringBuilder
appears to be more efficient in time and memory consumption only when the concatenation is done with 6-7 or more strings
.
不良习惯用法:
StringBuilder myString = new StringBuilder("123");
myString.Append("234");
myString.Append("345");
精练用法(注意使用if
):
Fine practice usage (note that if
is used):
StringBuilder myString = new StringBuilder("123");
if (Console.ReadLine() == "a")
{
myString.Append("234");
}
myString.Append("345");
最佳实践用法(注意使用了while
循环):
Best practice usage (note that while
loop is used):
StringBuilder myString = new StringBuilder("123");
while (Console.ReadLine() == "a")
{
myString.Append("234"); //Average loop times 4~ or more
}
myString.Append("345");
这篇关于如何明智地使用 StringBuilder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!