字符串格式和使用“+”构建字符串

发布于 2024-10-19 09:44:34 字数 1036 浏览 5 评论 0原文

我想问一下人们对编写字符串的想法是什么,以及构建字符串时性能是否存在巨大差异。

近年来,我一直被告知永远不要做以下事情:

string dogName = "Ralph";
DateTime bornDate = DateTime.Parse("2010-01-01");

string fullText = "I am a Dog and my name is " + dogName + " and i was born on the " + bornDate.ToString("dd/MM/yyyy");

并且总是被告知做如下类似的事情。

string dogName = "Ralph";
DateTime bornDate = DateTime.Parse("2010-01-01");

string fullText2 = String.Format("I am a Dog and my name is {0} and i was born on the {1:dd/MM/yyyy}", dogName, bornDate);

我明白使用后一个语句的原因,但是有人知道第一个语句存在什么类型的性能问题。

使用 StringBuilder 对象,而不是使用

string str = "Adding this comment to the string\n"
str += "Then Add this line";

和使用以下内容:

StringBuilder sb = new StringBuilder();

sb.AppendLine("Add This Comment");
sb.AppendLine("Then add this comment");

string output = sb.ToString();

我的团队中有一些开发人员,他们是有点老派的 VB6 程序员,我想向他们解释为什么这是一个坏主意。

他们不断地为代码中的 SQL 语句执行初始代码示例。

如果是的话当然:)

I want to ask what peoples thoughts are about writing strings and if there is a massive difference on performance when building a string.

I have always been told in recent years to never do the following:

string dogName = "Ralph";
DateTime bornDate = DateTime.Parse("2010-01-01");

string fullText = "I am a Dog and my name is " + dogName + " and i was born on the " + bornDate.ToString("dd/MM/yyyy");

And always told to something similar as below.

string dogName = "Ralph";
DateTime bornDate = DateTime.Parse("2010-01-01");

string fullText2 = String.Format("I am a Dog and my name is {0} and i was born on the {1:dd/MM/yyyy}", dogName, bornDate);

I see the reason for using the later statement, but does anyone know what type of performance issues there are with the first one.

And with the StringBuilder Object instead of using

string str = "Adding this comment to the string\n"
str += "Then Add this line";

and using the following:

StringBuilder sb = new StringBuilder();

sb.AppendLine("Add This Comment");
sb.AppendLine("Then add this comment");

string output = sb.ToString();

I have a handful of developers in my team at work that are a bit old school VB6 programmers and i want to explain to them why it a bad idea.

They do the Initial code example constantly for In Code SQL Statements.

If it is of course :)

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

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

发布评论

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

评论(3

护你周全 2024-10-26 09:44:34

字符串或字符串的串联操作的性能
StringBuilder 对象取决于内存分配发生的频率。
字符串连接操作总是分配内存,而
StringBuilder 连接操作仅在以下情况下分配内存:
StringBuilder 对象缓冲区太小,无法容纳新数据。
因此,String 类更适合串联
如果连接固定数量的 String 对象,则执行此操作。在
在这种情况下,各个串联操作甚至可能是
由编译器组合成单个操作。字符串生成器
如果任意一个对象对于串联操作来说是更可取的
连接的字符串数量;例如,如果一个循环
连接随机数量的用户输入字符串。

来源:http://msdn.microsoft.com/en-我们/library/system.text.stringbuilder.aspx

The performance of a concatenation operation for a String or
StringBuilder object depends on how often a memory allocation occurs.
A String concatenation operation always allocates memory, whereas a
StringBuilder concatenation operation only allocates memory if the
StringBuilder object buffer is too small to accommodate the new data.
Consequently, the String class is preferable for a concatenation
operation if a fixed number of String objects are concatenated. In
that case, the individual concatenation operations might even be
combined into a single operation by the compiler. A StringBuilder
object is preferable for a concatenation operation if an arbitrary
number of strings are concatenated; for example, if a loop
concatenates a random number of strings of user input.

Source: http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx

┊风居住的梦幻卍 2024-10-26 09:44:34

StringBuilder 实际上仅在构建需要大量连接的非常大的字符串时在性能方面有用。对于您提供的小样本,我认为使用 + 运算符就可以了。

当谈到 String.Format 时,我认为它应该用于提高代码的可读性或灵活性,但实际上并不是出于性能原因。 String.Format 还具有支持本地化的额外好处,这在使用 + 运算符时并不容易。

根据我的经验,我从未遇到过串联导致严重性能问题的情况,因此在尝试优化性能之前,我总是选择最可读的代码。

StringBuilder is really only useful in a performance sense when you are building a very large string which requires a lot of concatenation. For the small samples you provided I think that using the + operator is just fine.

When it comes to String.Format, I think it should be used to improve the readability or flexibility of the code but not really for performance reasons. String.Format also has the added benefit of supporting localization which is not really easy when using the + operator.

I have never run into a situation in my experience where concatenation was causing a serious perf problem so I always choose the most readable code before trying to optimize for performance.

霊感 2024-10-26 09:44:34

我知道 Java 实际上使用 StringBuffer(Java 版本的 StringBuilder)将一行中的 + 优化为语句,所以我猜 C# 可能也会做同样的事情。因此,它应该不会真正产生影响。仅当该行是程序中执行次数最多的行时,才真正值得查看差异(假设它存在)。

但是,当您使用 += 语句而不是使用 StringBuilder 构建整个页面时,差异非常明显;)我曾经重写了一个 ASP.NET C# 页面,该页面实际上都使用 += 构建(大型)SQL 查询作为整个响应而不是 StringBuilder。我将加载时间从 20 秒缩短到 <1 秒;)所以,是的,请尽量避免使用 += 来构建字符串。

I know that Java actually optimizes the +'s in one line to a statement using StringBuffer (Java's version of StringBuilder), so I guess C# probably does the same. Therefore it shouldn't really make a difference. Only if that one line is the most executed line in your program, it would be really worth looking at the difference (assuming it exists).

However, when you build an entire page using += statements instead of using a StringBuilderthe difference is quite noticeable ;) I once rewrote a ASP.NET C# page which actually both build up the (large) SQL queries as the entire response using += instead of StringBuilders. I got the loadtime down from 20 secs to <1 sec ;) So yes, do try to avoid += to build strings.

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