gpt4 book ai didi

c# - 不再需要 stringbuilder 了吗?

转载 作者:太空狗 更新时间:2023-10-29 22:32:39 25 4
gpt4 key购买 nike

我们都知道连接字符串会导致效率问题,尤其是在循环中。我被教导使用 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 更可取。

同时检查这个

StringBuilder is not always faster – Part 1 of 2

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 发表的评论中有一个很棒的链接,其中提到了

Rules of Thumb

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com