字符串格式和使用“+”构建字符串
我想问一下人们对编写字符串的想法是什么,以及构建字符串时性能是否存在巨大差异。
近年来,我一直被告知永远不要做以下事情:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来源:http://msdn.microsoft.com/en-我们/library/system.text.stringbuilder.aspx
Source: http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx
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.
我知道 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.