- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我们都知道连接字符串会导致效率问题,尤其是在循环中。我被教导使用 StringBuilder
来防止这些问题。
所以这样:
str += someotherstring
变成这样:
StringBuilder sb = new StringBuilder();
sb.AppendLine(someotherstring);
但据我了解,.NET Framework 3.5 及更高版本中的 CLR 足够智能,可以为两种方法输出相同的 IL。那么我是否应该再在我的团队代码审查中强制执行 stringbuilder?
编辑:我认为 Servy 在评论中一语中的:
This is the case when concatenating a number of strings known at compile time. Because of that, when concatenating a number of strings known at compile time there is no need to use a SB. When concatenating a number of strings unknown at compile time, it cannot do that
最佳答案
不,这并不总是正确的。我不知道,如果你检查过这个How to improve string concatenation performance in Visual C#
However, the .NET Framework includes a StringBuilder class that is optimized for string concatenation. It provides the same benefits as using a character array in C/C++, as well as automatically growing the buffer size (if needed) and tracking the length for you. The sample application in this article demonstrates the use of the StringBuilder class and compares the performance to concatenation.
当您在代码传递中执行多个循环或分支时,StringBuilder 更可取。
同时检查这个
This block of code took 1484 milliseconds to run on my PC:
for (int i = 0; i <= 1000000; i++) {
// Concat strings 3 times using StringBuilder
StringBuilder s = new StringBuilder();
s.Append(i.ToString());
s.Append(i.ToString());
s.Append(i.ToString()); }And this one, using traditional concatenation, took slightly less time (1344 milliseconds):
for (int i = 0; i <= 1000000; i++) {
// Concat strings 3 times using traditional concatenation
string s = i.ToString();
s = s + i.ToString();
s = s + i.ToString(); }The above data suggests that StringBuilder only starts to work faster once the number of concatenations exceed 3.
Tim 发表的评论中有一个很棒的链接,其中提到了
So, when should you use StringBuilder, and when should you use the string concatenation operators?
- 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.
关于c# - 不再需要 stringbuilder 了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19593508/
公共(public)类TestMyStringBuilderII { public static void main(String[] args) { StringBuilder sb = n
我们都知道字符串是不可变的而StringBuilder是可变的。正确的。那么为什么它的方法会返回一个 StringBuilder 对象。它们不应该都是 void 方法吗? 为什么会这样 public
当我读到 equals() 方法用于比较 java 中的字符串是否相等但是当我运行这段代码时我得到输出 false 。为什么? public class TestStringBuilder { pu
我收到一份模型列表。模型的数量可能很大。这个模型有一堆属性,其中任何一个都可能是null。 我需要根据模型的属性为每个模型构建一个字符串。如果 property == null,那么我会在结果字符串中
我写了一些代码,其中有很多字符串创建。为了尽量避免一遍又一遍地创建昂贵的字符串,我使用了 java StringBuilder 类。 我的代码有点像这样: public String createSt
这个问题在这里已经有了答案: 关闭10年前。 Possible Duplicate: StringBuilder vs String concatenation in toString() in Ja
类 StringBuilder 定义了四个构造函数,它们都不接受 StringBuilder,但以下编译: StringBuilder sb = new StringBuilder(new Strin
我正在努力在我的代码中使用 StringBuilder,而不是 String使代码在所有解析和连接过程中具有时间效率。 但是当我查看它的源代码时,substring() 方法AbstractStrin
我想知道 StringBuilder,我有一个问题希望社区能够解释。 让我们忘掉代码的可读性,哪些是更快,为什么? StringBuilder.Append: StringBuilder sb = n
这个问题在这里已经有了答案: Single exclamation mark in Kotlin (6 个回答) Example of when should we use run, let, app
我已经附加了一些带有一些字符的 strBuild 并将该 strBuild 放入 stringBuildArr 中。然后在strBuild上调用java.lang.StringBuilder.dele
在 C# 中哪个内存效率更高:选项 #1 还是选项 #2? public void TestStringBuilder() { //potentially a collection with
当使用 StringBuilder.ToString() 时,我遇到了 OutOfMemory 异常。因为我的字符串需要很大的空间。这就是为什么我需要一种方法(也许通过流式传输)来让它工作。 这是我的
这个问题已经有答案了: What's the difference between instance method reference types in Java 8? (3 个回答) 已关闭 7 年
这个问题在这里已经有了答案: What's the difference between instance method reference types in Java 8? (3 个答案) 关闭
最近的 question came up关于使用 String.Format()。我的部分回答包括使用 StringBuilder.AppendLine(string.Format(...)) 的建议
在 SO 上看到一个关于连接字符串的问题后,我做了一些测试,了解到在 foreach 中连接字符串比使用 for 循环和使用数组中的索引慢。由于对数组进行绑定(bind)检查,for 循环不应该变慢吗
我的问题是如果我有时在同一个字符串上使用多线程 字符串不会被替换。(我在记事本上写了这个,所以语法可能是 错误) 使用 System.Thread ...当然还有其他 class .... {
如果我创建一个不可变类。所有字段都必须是最终的。如果我像这样使用 stringbuilder final StringBuilder s = new StringBuilder("你好"); ,那么
是否可以配置 Java StringBuilder类在每次调用之间追加一个新行以追加? 最佳答案 我不认为 StringBuilder内置了该功能。既然你不能只是扩展它,你可以尝试使用装饰器模式...
我是一名优秀的程序员,十分优秀!