什么时候使用StringBuilder?
可能的重复:
字符串与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我有一篇关于这个主题的文章。 总之(从页面底部复制):
I've got an article on this very topic. In summary (copied from the bottom of the page):
这是我的经验法则:
Here is my rule of thumb:
Coding Horror 有一篇关于这个问题的好文章,微优化剧院的悲惨悲剧。
Coding Horror has a good article concerning this question, The Sad Tragedy of Micro-Optimization Theater.
就个人而言,当我要连接的字符串不止一两个时,我会使用 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.有人通过实验发现关键数字是 6。连续超过 6 个串联,您应该使用 StringBuilder。 不记得在哪里找到这个了。
但是,请注意,如果您只是将其写在一行中:
它将转换为一个函数调用(我不知道如何在 VB.net 中编写它)
因此,如果您在一行上进行所有串联,那么就不要这样做不用担心 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:
That gets converted into one function call (I don't know how to write it in VB.net)
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.
我的规则 - 当您在 For 或 Foreach 循环中添加字符串时,请使用 StringBuilder。
My rule - when you're adding to a string in a For or Foreach loop, use the StringBuilder.