什么时候使用StringBuilder?

发布于 2024-07-13 22:15:30 字数 566 浏览 5 评论 0原文

可能的重复:
字符串与 StringBuilder

我刚刚重温了一些我用来学习 VB.NET 的书籍。 我不确定我是否已经理解了这一点,了解 StringBuilder 是如何/什么的。

使用指导是什么? 如果您要连接 2 个字符串还是 50 个字符串,最好使用它吗?

或者当字符串总长度大于128个字符时?

或者,每当您使用它来将字符串添加在一起时,您是否会看到性能优势?

在哪种情况下,使用 StringBuilder 实例构建 SQL 语句比使用 string.format("Select * from x where y = {0}",1) 更好?

我总是觉得声明另一个变量并包含名称空间对于小字符串连接没有好处,但我现在不确定。

抱歉,很多文档告诉您应该使用什么,但没有告诉您什么是最好的。

Possible Duplicate:
String vs StringBuilder

I just revisited some of the books that I used to pick up VB.NET. I am not sure I've got this in my head, understand how/what StringBuilder is.

What is the guidance for using? Is it best to use it if you are are concatenating 2 strings or 50?

Or when the the total string length is greater than 128 characters?

Or will you see a performance benefit whenever you use it to add strings together?

In which case is it better to use a StringBuilder instance to build a SQL statement than string.format("Select * from x where y = {0}",1)?

It's always struck me that declaring another variable and including a name space is not beneficial for small string concatenations, but I am not sure now.

Sorry, lot of documentation tells you what to use, just not what's best.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

和我恋爱吧 2024-07-20 22:15:30

我有一篇关于这个主题的文章。 总之(从页面底部复制):

  • 当您在一个不平凡的循环中连接时,一定要使用 StringBuilder - 特别是如果您不确定(在编译时)将通过该循环进行多少次迭代环形。 例如,一次读取文件一个字符,使用 += 运算符构建字符串可能是性能自杀。
  • 当您可以(可读地)指定需要在一条语句中连接的所有内容时,一定要使用连接运算符。 (如果您有一个要连接的数组,请考虑显式调用 String.Concat - 或者如果您需要分隔符,请考虑调用 String.Join。)
  • 不要害怕将文字分成几个连接位 - 结果将是相同的。 例如,您可以通过将长文字分成几行来提高可读性,而不会损害性能。
  • 如果您需要连接的中间结果而不是提供下一次连接迭代,那么 StringBuilder 将无法帮助您。 例如,如果您从名字和姓氏构建一个全名,然后在末尾添加第三条信息(可能是昵称),则只有在不这样做的情况下,您才会从使用 StringBuilder 中受益需要 (名字 + 姓氏) 字符串用于其他目的(正如我们在创建 Person 对象的示例中所做的那样)。
  • 如果您只需要进行一些串联,并且您确实想在单独的语句中进行这些操作,那么采用哪种方式并不重要。 哪种方式更有效取决于连接的数量、所涉及的字符串的大小以及连接的顺序。如果您确实认为该代码段是性能瓶颈,请对它进行分析或基准测试。

I've got an article on this very topic. In summary (copied from the bottom of the page):

  • Definitely use StringBuilder when you're concatenating in a non-trivial loop - especially if you don't know for sure (at compile time) how many iterations you'll make through the loop. For example, reading a file a character at a time, building up a string as you go using the += operator is potentially performance suicide.
  • Definitely use the concatenation operator when you can (readably) specify everything which needs to be concatenated in one statement. (If you have an array of things to concatenate, consider calling String.Concat explicitly - or String.Join if you need a delimiter.)
  • Don't be afraid to break literals up into several concatenated bits - the result will be the same. You can aid readability by breaking a long literal into several lines, for instance, with no harm to performance.
  • If you need the intermediate results of the concatenation for something other than feeding the next iteration of concatenation, StringBuilder isn't going to help you. For instance, if you build up a full name from a first name and a last name, and then add a third piece of information (the nickname, maybe) to the end, you'll only benefit from using StringBuilder if you don't need the (first name + last name) string for other purpose (as we do in the example which creates a Person object).
  • If you just have a few concatenations to do, and you really want to do them in separate statements, it doesn't really matter which way you go. Which way is more efficient will depend on the number of concatenations the sizes of string involved, and what order they're concatenated in. If you really believe that piece of code to be a performance bottleneck, profile or benchmark it both ways.
赢得她心 2024-07-20 22:15:30

这是我的经验法则:

StringBuilder 最适合在编译时未知连接的确切数量时使用。

Here is my rule of thumb:

StringBuilder is best used when the exact number of concatenations is unknown at compile time.

野却迷人 2024-07-20 22:15:30

Coding Horror 有一篇关于这个问题的好文章,微优化剧院的悲惨悲剧

Coding Horror has a good article concerning this question, The Sad Tragedy of Micro-Optimization Theater.

故事未完 2024-07-20 22:15:30

就个人而言,当我要连接的字符串不止一两个时,我会使用 StringBuilder。 我不确定是否会获得真正的性能提升,但我总是读到并被告知,每次对多个字符串进行常规串联都会创建一个额外的字符串副本,而使用 StringBuilder 会保留一个副本复制直到调用最终的ToString() 方法。

Personally I use StringBuilder when I have more than just one or two strings to concatenate. I'm not sure if there's a real performance hit to be gained, but I've always read and been told that doing a regular concatenation with multiple strings creates an extra copy of the string each time you do it, while using StringBuilder keeps one copy until you call the final ToString() method on it.

你与昨日 2024-07-20 22:15:30

有人通过实验发现关键数字是 6。连续超过 6 个串联,您应该使用 StringBuilder。 不记得在哪里找到这个了。

但是,请注意,如果您只是将其写在一行中:

"qwert" + "yuiop" + "asdf" + "gh" + "jkl;" + "zxcv" + "bnm" + ",."

它将转换为一个函数调用(我不知道如何在 VB.net 中编写它)

String.Concat("qwert", "yuiop", "asdf", "gh", "jkl;", "zxcv", "bnm", ",.");

因此,如果您在一行上进行所有串联,那么就不要这样做不用担心 StringBuilder,因为 String.Concat 会有效地一次性完成所有连接。 仅当您在循环中执行它们或连续连接它们时才如此。

Someone's figured out experimentally that the critical number is 6. More than 6 concatenations in a row and you should use a StringBuilder. Can't remember where I found this.

However, note that if you just write this in a line:

"qwert" + "yuiop" + "asdf" + "gh" + "jkl;" + "zxcv" + "bnm" + ",."

That gets converted into one function call (I don't know how to write it in VB.net)

String.Concat("qwert", "yuiop", "asdf", "gh", "jkl;", "zxcv", "bnm", ",.");

So if you're doing all concatenations on one line, then don't bother with StringBuilder because String.Concat effectively will do all the concatenations in one go. It's only if you're doing them in a loop or successively concatenating.

魂ガ小子 2024-07-20 22:15:30

我的规则 - 当您在 For 或 Foreach 循环中添加字符串时,请使用 StringBuilder。

My rule - when you're adding to a string in a For or Foreach loop, use the StringBuilder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文