string 何时使用 StringBuilder?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/529999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use StringBuilder?
提问by spacemonkeys
Possible Duplicate:
String vs StringBuilder
可能的重复:
String 与 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.
我刚刚重温了一些我用来学习 VB.NET 的书。我不确定我的脑子里是否有这个,了解 StringBuilder 是如何/什么是。
What is the guidance for using? Is it best to use it if you are are concatenating 2 strings or 50?
使用指导是什么?如果要连接 2 个字符串或 50 个字符串,最好使用它吗?
Or when the the total string length is greater than 128 characters?
或者当总字符串长度大于 128 个字符时?
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)
?
在哪种情况下,使用 StringBuilder 实例构建 SQL 语句比使用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.
抱歉,很多文档会告诉您使用什么,而不是最好的。
回答by Jon Skeet
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.
- 当您在一个非平凡的循环中进行连接时,一定要使用 StringBuilder - 特别是如果您不确定(在编译时)您将通过循环进行多少次迭代。例如,一次读取一个文件,使用 += 运算符构建一个字符串,这可能是性能自杀。
- 当您可以(可读地)指定需要在一个语句中连接的所有内容时,一定要使用连接运算符。(如果您有一系列要连接的内容,请考虑显式调用 String.Concat - 如果需要分隔符,请考虑调用 String.Join。)
- 不要害怕将文字分解成几个串联的位 - 结果将是相同的。例如,您可以通过将长文字分成几行来提高可读性,而不会对性能造成损害。
- 如果您需要连接的中间结果而不是提供下一次连接迭代,StringBuilder 不会帮助您。例如,如果您从一个名字和一个姓氏建立一个全名,然后在最后添加第三条信息(可能是昵称),那么只有在您不这样做的情况下才可以从使用 StringBuilder 中受益需要(名字 + 姓氏)字符串用于其他目的(就像我们在创建 Person 对象的示例中所做的那样)。
- 如果您只需要执行几个连接,并且您真的想在单独的语句中执行它们,那么走哪条路并不重要。哪种方式更有效将取决于所涉及的字符串大小的串联次数,以及它们串联的顺序。如果您真的认为那段代码是性能瓶颈,请以两种方式对其进行配置文件或基准测试。
回答by Andrew Hare
Here is my rule of thumb:
这是我的经验法则:
StringBuilder
is best used when the exact number of concatenations is unknown at compile time.
StringBuilder
最好在编译时不知道确切的连接数时使用。
回答by VBNight
Coding Horrorhas a good article concerning this question, The Sad Tragedy of Micro-Optimization Theater.
Coding Horror有一篇关于这个问题的好文章,The Sad Tragedy of Micro-Optimization Theatre。
回答by Brandon
My rule - when you're adding to a string in a For or Foreach loop, use the StringBuilder.
我的规则 - 当您在 For 或 Foreach 循环中添加字符串时,请使用 StringBuilder。
回答by Wayne Molina
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.
就个人而言,当我有不止一两个字符串要连接时,我会使用 StringBuilder。我不确定是否会获得真正的性能影响,但我一直读到并被告知,每次执行与多个字符串的常规连接都会创建一个额外的字符串副本,而使用 StringBuilder 会保留一个复制直到你调用它的最后一个ToString()
方法。
回答by Ray Hidayat
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.
有人通过实验发现临界数字是 6。连续超过 6 个串联,您应该使用 StringBuilder。不记得我在哪里找到这个的。
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)
这被转换为一个函数调用(我不知道如何在 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.
因此,如果您在一行上进行所有连接,则不要使用 StringBuilder,因为 String.Concat 将有效地一次性完成所有连接。仅当您在循环中执行它们或连续连接时。