StringBuilder性能与字符串连接 [英] StringBuilder Performance vs. String Concatenation

查看:57
本文介绍了StringBuilder性能与字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速问题:


StringBuilder显然更有效处理字符串连接

比旧的''+ =''方法...但是,在处理相对较大的字符串

连接(即20-30k)时,在初始化一个新的字符串之间有什么性能差异(如果有的话,这是一个微不足道的b $ b)

具有指定容量的StringBuilder与初始化新实例

没有...(最终长度不固定)


ie,


性能差异:


StringBuilder sb1 = new StringBuilder(30000);





StringBuilder sb1 = new StringBuilder();


与''+ ='相比,这是否会产生巨大影响'?


干杯,

-k

Quick Question:

StringBuilder is obviously more efficient dealing with string concatenations
than the old ''+='' method... however, in dealing with relatively large string
concatenations (ie, 20-30k), what are the performance differences (if any
with something as trivial as this) between initializing a new instance of
StringBuilder with a specified capacity vs. initializing a new instance
without... (the final length is not fixed)

ie,

Performance differences between:

StringBuilder sb1 = new StringBuilder(30000);

and

StringBuilder sb1 = new StringBuilder();

Does this make a huge difference compared to ''+=''?

Cheers,
-k

推荐答案

如果你是做大字符串连接,绝对使用

StringBuild呃。这本身没有神奇的数字,但是在琐碎的
连接中,这并不是什么大不了的事。要记住的主要事情是

字符串是不可变的,所以50个连接创建50个字符串对象。


如果你初始化SB,你最好是你超过了默认大小

,因为它不需要重新分配空间,但这只会发挥作用

如果超过默认大小。如果超出界限,两者之间的性能差异实际上只会被注意到,所以很难说

绝对值,这取决于具体情况。如果可能的话,尝试初始化

的容量,但即使你不是,你也会比+ =好多了。


HTH,


Bill

" Kevin C" <柯************ @ sbcglobal.net>在消息中写道

新闻:E0 **************** @ newssvr27.news.prodigy.com ...
IF you are doing large string concatenations, definitely use the
StringBuilder. There''s no magic number per se, but on trivial
concatentions, it''s not a big deal. The main thing to remember is that
strings are immutable, so 50 concatenations creates 50 string objects.

If you initialize the SB, you are better off if you go over the default size
because it doesn''t have to reallocate space, but this only comes into play
if you exceed the default size. The performance differences really only are
noticed between the two if you exceed the boundaries, so it''s hard to say in
absolute terms, it depends on the situation. If possible, try to initialize
the capacity, but even if you don''t, you''ll be much better off than +=.

HTH,

Bill
"Kevin C" <ke************@sbcglobal.net> wrote in message
news:E0****************@newssvr27.news.prodigy.com ...
快速问题:

StringBuilder显然比旧的''+ =''方法处理字符串
连接更有效...但是,在处理相对较大的
字符串连接时( ,即20-30k),在初始化具有指定容量的StringBuilder的新实例与初始化新实例之间有什么性能差异(如果有的话,这些内容很简单)
没有......(最终长度不固定)



性能差异:

StringBuilder sb1 =新的StringBuilder(30000) );



StringBuilder sb1 = new StringBuilder();

与+ =相比,这是否会产生巨大的差异?

干杯,
-k
Quick Question:

StringBuilder is obviously more efficient dealing with string concatenations than the old ''+='' method... however, in dealing with relatively large string concatenations (ie, 20-30k), what are the performance differences (if any
with something as trivial as this) between initializing a new instance of
StringBuilder with a specified capacity vs. initializing a new instance
without... (the final length is not fixed)

ie,

Performance differences between:

StringBuilder sb1 = new StringBuilder(30000);

and

StringBuilder sb1 = new StringBuilder();

Does this make a huge difference compared to ''+=''?

Cheers,
-k



账单,


感谢您的快速回复 - tha这是有道理的。


尽管如此,它对

表现有最负面(理论上?)的影响:


1)初始化一个实例,即设置容量为30,000个字符

当你只需要20,000个



2 )在初始化一个实例时,即设置10,000个字符的容量

并且当你试图附加20,000个时,让StringBuilder类为

额外10,000个字符动态分配更多空间。


-k


" William Ryan" <做******** @ comcast.nospam.net>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
bill,

thanks for the quick reply -- that makes sense.

As a footnote though, which has the most negative (theorical?) effect on
performance:

1) over initializing an instance, ie., setting capacity at 30,000 characters
when you only need 20,000
or
2) under initialzing an instance, ie., setting capacity at 10,000 characters
and having the StringBuilder class dynamically allocate more room for the
additional 10,000 characters when you try to append 20,000.

-k


"William Ryan" <do********@comcast.nospam.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
如果你正在做大字符串连接,绝对使用
StringBuilder。这本身没有神奇的数字,但是在琐碎的连接上,这并不是什么大不了的事。要记住的主要事情是
字符串是不可变的,所以50个连接会创建50个字符串对象。

如果你初始化SB,如果你超过默认的$ b $,你会更好。 b尺寸,因为它不需要重新分配空间,但只有当你超过默认尺寸时才会发挥作用。如果超出界限,两者之间的性能差异实际上只有
,所以很难说绝对值为
,这取决于具体情况。如果可能的话,尝试
初始化容量,但即使你不是,你也会比+ =好多了。

HTH,
凯文C <柯************ @ sbcglobal.net>在消息中写道
新闻:E0 **************** @ newssvr27.news.prodigy.com ...
IF you are doing large string concatenations, definitely use the
StringBuilder. There''s no magic number per se, but on trivial
concatentions, it''s not a big deal. The main thing to remember is that
strings are immutable, so 50 concatenations creates 50 string objects.

If you initialize the SB, you are better off if you go over the default size because it doesn''t have to reallocate space, but this only comes into play
if you exceed the default size. The performance differences really only are noticed between the two if you exceed the boundaries, so it''s hard to say in absolute terms, it depends on the situation. If possible, try to initialize the capacity, but even if you don''t, you''ll be much better off than +=.

HTH,

Bill
"Kevin C" <ke************@sbcglobal.net> wrote in message
news:E0****************@newssvr27.news.prodigy.com ...
快速问题:

StringBuilder处理字符串
Quick Question:

StringBuilder is obviously more efficient dealing with string


串联

显然比旧的''+ =''方法更有效...但是,相对于大
than the old ''+='' method... however, in dealing with relatively large


字符串

连接(即20-30k),在初始化一个新的之间有什么性能差异(如果
,任何事情都有这么简单)具有指定容量的StringBuilder的实例
与初始化新实例
没有...(最终长度不固定)



>性能差异:

StringBuilder sb1 = new StringBuilder(30000);



StringBuilder sb1 = new StringBuilder();

与+ =相比,这是否会产生巨大的影响?

干杯,
-k
concatenations (ie, 20-30k), what are the performance differences (if any with something as trivial as this) between initializing a new instance of StringBuilder with a specified capacity vs. initializing a new instance
without... (the final length is not fixed)

ie,

Performance differences between:

StringBuilder sb1 = new StringBuilder(30000);

and

StringBuilder sb1 = new StringBuilder();

Does this make a huge difference compared to ''+=''?

Cheers,
-k




" Kevin C" <柯************ @ sbcglobal.net>写在

新闻:LB ***************** @newssvr27.news.prodigy.co m:
"Kevin C" <ke************@sbcglobal.net> wrote in
news:LB*****************@newssvr27.news.prodigy.co m:

尽管脚注对性能有最负面的(理论上的?)影响:

1)初始化一个实例,即设置容量为30,000
当你只需要20,000个

2)初始化一个实例时,即设置容量为10,000
字符并让StringBuilder类动态分配
更多当你试图追加额外的10,000个字符的空间
20,000。

As a footnote though, which has the most negative (theorical?) effect
on performance:

1) over initializing an instance, ie., setting capacity at 30,000
characters when you only need 20,000
or
2) under initialzing an instance, ie., setting capacity at 10,000
characters and having the StringBuilder class dynamically allocate
more room for the additional 10,000 characters when you try to append
20,000.




我会说数字2的负面影响最大。当你附加超过StringBuilder容量的
字符时,它必须分配足够大的内存以保存新字符串,将现有字符复制到

然后添加新字符。虽然在数量上,分配是

已经做了,它只需附加新数据。


Chris



I would say that number 2 has the most negative impact. When you append
characters that exceed the capacity of the StringBuilder, it must allocate
memory large enough to hold the new string, copy the existing characters to
it and then add the new characters. Whereas on number, the allocation is
already do and it just has to append the new data.

Chris


这篇关于StringBuilder性能与字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆