StringBuilder问题 [英] StringBuilder question

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

问题描述

假设我们已经定义了StringBuilder myBuilder,以下哪个是最贵的:


//方案1

myBuilder.Append(" test" +" test" +" test");


//场景2

myBuilder.Append(" test");

myBuilder.Append(" test");

myBuilder.Append(" test");

Assuming we''ve defined StringBuilder myBuilder, which of the following is most expensive:

//scenario 1
myBuilder.Append("test" + "test" + "test");

//scenario 2
myBuilder.Append("test");
myBuilder.Append("test");
myBuilder.Append("test");

推荐答案

Jerry,


你的意思是更多的线路价格昂贵?还是性能?


我想说正确的方法可能是这样的。
myBuilder.Append(" test")。 (test1)。追加(t est2);


Shak。

Jerry <济*** @ discussions.microsoft.com>在消息中写道

news:24 ********************************** @ microsof t.com ...
Jerry,

you mean more number of lines as expensive? or the performance?

I would say the correct way to do that may be

myBuilder.Append("test").Append("test1").Append("t est2");

Shak.
"Jerry" <Je***@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
假设我们已经定义了StringBuilder myBuilder,以下哪个是
最贵的:
//场景1
myBuilder.Append (测试+测试+测试);

//方案2
myBuilder.Append(" test");
myBuilder.Append (test);
myBuilder.Append(" test");
Assuming we''ve defined StringBuilder myBuilder, which of the following is most expensive:
//scenario 1
myBuilder.Append("test" + "test" + "test");

//scenario 2
myBuilder.Append("test");
myBuilder.Append("test");
myBuilder.Append("test");



场景1,因为字符串的新副本正在为每个+

操作创建(这就是为什么首先有一个StringBuilder类)。


然而,这是可能的(因为常量折叠)实际上可能是
编译成myBuilding.Append(testtesttest);,在这种情况下,第二个
场景会更贵。 br />

-

John Wood

电子邮件:名字,点,姓氏,来自priorganize.com


" Jerry" <济*** @ discussions.microsoft.com>在消息中写道

news:24 ********************************** @ microsof t.com ...
scenario 1, because a new copy of the string is being created for each +
operation (which is why there is a StringBuilder class in the first place).

However, it''s possible (because of constant folding) that may actually
compile to be myBuilding.Append("testtesttest");, in which case the second
scenario would be more expensive.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Jerry" <Je***@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
假设我们已经定义了StringBuilder myBuilder,以下哪个是
最贵的:
//场景1
myBuilder.Append (测试+测试+测试);

//方案2
myBuilder.Append(" test");
myBuilder.Append (test);
myBuilder.Append(" test");
Assuming we''ve defined StringBuilder myBuilder, which of the following is most expensive:
//scenario 1
myBuilder.Append("test" + "test" + "test");

//scenario 2
myBuilder.Append("test");
myBuilder.Append("test");
myBuilder.Append("test");



当然,两者都比


someString =" test" +测试 +测试;


两行代码和性能。


" John Wood" < j@ro.com>在消息中写道

news:ey ************** @ tk2msftngp13.phx.gbl ...
And of course, both are more expensive than

someString = "test" + "test" + "test";

in both lines of code and performance.

"John Wood" <j@ro.com> wrote in message
news:ey**************@tk2msftngp13.phx.gbl...
方案1,因为a正在为每个+
操作创建字符串的新副本(这就是为什么在第一个
位置有一个StringBuilder类)。
然而,它是可能的(因为常量)折叠)实际上可能编译成myBuilding.Append(testtesttest);,在这种情况下第二种情况会更贵。

-
<济*** @ discussions.microsoft.com>在消息中写道
新闻:24 ********************************** @ microsof t.com。 ..
scenario 1, because a new copy of the string is being created for each +
operation (which is why there is a StringBuilder class in the first place).
However, it''s possible (because of constant folding) that may actually
compile to be myBuilding.Append("testtesttest");, in which case the second
scenario would be more expensive.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Jerry" <Je***@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
假设我们已经定义了StringBuilder myBuilder,以下哪个
Assuming we''ve defined StringBuilder myBuilder, which of the following


最贵:


//场景1
myBuilder.Append(" test" +" test" +" test");

//场景2
myBuilder.Append(" test") ;
myBuilder.Append(" test");
myBuilder.Append(" test");

//scenario 1
myBuilder.Append("test" + "test" + "test");

//scenario 2
myBuilder.Append("test");
myBuilder.Append("test");
myBuilder.Append("test");




这篇关于StringBuilder问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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