03/02: String.Format && StringBuilder && String.Concat Performance
The performance for string.format is not good. Its allows code to be more easily localized.
- string.Format takes LOT OF TIME. It has to parse the string, replace in that strings str1,str2 and str3. It's 3-4 times slower than concatenation.
- String.Format() actually creates a StringBuilder and calls StringBuilder.AppendFormat()! String.Format() is implemented like this:
String.Concat is better for performance.
(ZT:http://blog.csdn.net/zhanglei5415/archive/2007/07/23/1703256.aspx)
以下两段语句最终效果均为用数据库插入数据,请先对比观察它们的不同:
string sql = string.Concat(new object[] {"insert into 表名(字段1,字段2) values('",值1,"','",值2,"')"});
string sql = "insert into 表名(字段1,字段2) values('"+值1+"','"+值2+"')";
以上两条语句的结果是相同的,但采用string.Concat()可以使用程序的执行效率显著提高,所以,建议拼串的操作都采用string.Concat来实现。
Conclusion
String.format< stringbuidler.append (as string.format will call stringbuilder.append)<String.concate
0 Comments:
Post a Comment
<< Home