教你50招提升ASP.NET性能(二十三):StringBuilder不适用于所有字符串连接的场景;String.Join可能是
(41)StringBuilder is NOT the answer for all string concatenation scenarios; String.Join could be
招数41:
StringBuilder不适用于所有字符串连接的场景;String.Join可能是
Yes, if you are in a loop and adding to a string, then a StringBuilder *could* be most appropriate. However, the overhead of spinning up a StringBuilder instance makes the following pretty dumb:
是的,如果你是在一个循环中并添加到一个字符串,那么StringBuilder可能是最适合的。然而,创建一个StringBuilder实例的开销让下面的代码相当愚蠢的。
var sb = new StringBuilder();
sb.Append(“Frankly, this is “);
sb.Append(notMoreEfficient);
sb.Append(“. Even if you are in a loop.”);
var whyNotJustConcat = sb.ToString();
Instead, use String.Join, which is typically more performant than spinning up a StringBuilder instance for a limited number of strings. It’s my go-to concat option:
相反,对于一个有限数量的字符串使用String.Join比创建一个StringBuilder实例通常是更具表现力的。这是我首选字符串连接方式:
string key = String.Join(“ “, new String[]
{ “This”, “is”, “a”, “much”, “better”,
solution, “.”});
The first variable of " " can just be set to "" when you don’t want a delimiter.
当你不想要一个分隔符时候,第一个变量的“ ”可以被设置为“”。
For loops that do a lot of, er, looping, sure, use a StringBuilder.
循环中那么做就多余了,嗯,循环,当然是用StringBuilder。
Just don’t assume it’s the de facto solution in all, or even the majority of cases. My rule of thumb is to add strings together when I’ve got one to five of them (likewise with String.Format if it helps with legibility). For most other cases, I tend towards String.Join. Only when dealing with a loop that isn’t limited to about 10 iterations, especially one that really lets rip, do I spin up a StringBuilder.
只要不认为他不是所有的解决方案,甚至大多数情况下。我的原则是当我有1到5个字符连接的时候(同样是String.Format如果他有助于易读性) 对于其他大多数情况下,我倾向于String.Join。只有当处理一个并不限于10次迭代的循环,特别是...,我会使用StringBuilder。
教你50招提升ASP.NET性能(二十三):StringBuilder不适用于所有字符串连接的场景;String.Join可能是的更多相关文章
- 教你50招提升ASP.NET性能(十三):精选技巧集合
(19)A selection of tips 招数19: 精选技巧集合 Including height and width in <img /> tags will allow you ...
- 教你50招提升ASP.NET性能(二十六):对于开发人员的数据库性能技巧
Database Performance Tips for Developers对于开发人员的数据库性能技巧 As a developer you may or may not need to go ...
- 教你50招提升ASP.NET性能(十六):把问题仍给硬件而不是开发人员
(27)Throw hardware at the problem, not developers 招数27: 把问题仍给硬件而不是开发人员 As developers, we often want ...
- 教你50招提升ASP.NET性能(十一):避免在调试模式下运行网站
(17)Avoid running sites in debug mode 招数17: 避免在调试模式下运行网站 When it comes to ASP.NET, one of the most c ...
- 教你50招提升ASP.NET性能(七):总是在服务器端执行验证
(13)Always perform validation on the server as well 招数13: 总是在服务器端执行验证 This isn’t exactly a performan ...
- 教你50招提升ASP.NET性能(二):移除不用的视图引擎
(2)Remove unused View Engines 招数2: 移除不用的视图引擎 If you're an ASP.NET MVC developer, you might not know ...
- 教你50招提升ASP.NET性能(二十四):ORM小窍门
ORM TipsORM小窍门 More and more people are using Object to Relational Mapping (ORM) tools to jump the d ...
- 教你50招提升ASP.NET性能(二十一):避免使用会话状态
(39)Avoid using session state 招数39: 避免使用会话状态 Where possible, you should try and avoid using session ...
- 教你50招提升ASP.NET性能(二十):7条便利的ViewState技巧
(32)Seven handy ViewState tips 招数32: 7条便利的ViewState技巧 Every time I have to deal with a classic ASP.N ...
- 教你50招提升ASP.NET性能(十九):静态集合
(30)Static collections 招数30: 静态集合 If a collection is static, make sure it only contains the objects ...
随机推荐
- I.MX6 git patch
/********************************************************************** * I.MX6 git patch * 说明: * 之前 ...
- Java [Leetcode 136]Single Number
题目描述: Given an array of integers, every element appears twice except for one. Find that single one. ...
- Mysqlbackup 备份详解(mysql官方备份工具)
A.1全库备份. 命令: mysqlbackup --defaults-file=/home/mysql-server/mysql3/my.cnf --user=root --password=ro ...
- TCP/IP详解学习笔记(6)-UDP协议
1.UDP简要介绍 UDP是传输层协议,和TCP协议处于一个分层中,但是与TCP协议不同,UDP协议并不提供超时重传,出错重传等功能,也就是说其是不可靠的协议. 2.UDP协议头 2.1.UDP端口号 ...
- 【转】android布局属性详解
LinearLayout布局: 线性版面配置,在这个标签中,所有元件都是按由上到下的排队排成的.在这个界面中,我们应用了一个 LinearLayout的布局,它是垂直向下扩展的 ,所以创建的布局XML ...
- Delphi 2010
Delphi 2010已早由Embarcadero公司发布.作者Kim Madsen作为一名资深的Delphi开发者,在他的博客中谈到了Delphi 2010的新性能.它的使用感受以及对Delphi语 ...
- poco网络库分析,教你如何学习使用开源库
Poco::Net库中有 FTPClient HTML HTTP HTTPClient HTTPServer ICMP Logging Mail Messages NetCore NTP OAuth ...
- C++11笔记
__func__宏 __func__返回当前的函数名,也可以返回class和struct名. /*返回函数名hello*/ const char* hello() { return __func__; ...
- DzzOffice添加动态壁纸例子-Bing每日壁纸
Bing每日壁纸介绍:bing网站每天会更新一张不同的精选图片. 此压缩包内的程序,可以自动同步更新cn.bing.com网站每天更新的图片,作为dzzoffice的壁纸使用.实现自动每天更换不同的云 ...
- ASM中的别名
在ASM中的别名,是为了方便管理. 在ASM中创建别名,一种是在RDBMS中创建,另外一种是在ASM中创建,区别就是在ASM中创建的别名,在RDBMS中是不可见的. 在RDBMS中创建别名: SQL& ...