首先看测试代码:

   public class StringSpeedTest
{
private readonly static string _testStr = ""; public string StringAdd(int count)
{
string str = string.Empty;
for (int i = ; i < count; i++)
{
str += _testStr;
}
return str;
} public string UseStringBuilder(int count)
{
StringBuilder sb = new StringBuilder(); for (int i = ; i < count; i++)
{
sb.Append(_testStr);
} return sb.ToString();
}
public string UseStringBuilderWithCapacity(int count)
{
StringBuilder sb = new StringBuilder(count * _testStr.Length); for (int i = ; i < count; i++)
{
sb.Append(_testStr);
} return sb.ToString();
}
public string UseStringConcat(int count)
{
string[] list = new string[count];
for (int i = ; i < count; i++)
{
list[i] = _testStr;
}
return string.Concat(list);
}
public string UseStringConcatWithList(int count)
{
List<string> list = new List<string>(count);
for (int i = ; i < count; i++)
{
list.Add(_testStr);
}
return string.Concat(list);
}
public string UseStringConcatWithListToArray(int count)
{
List<string> list = new List<string>(count);
for (int i = ; i < count; i++)
{
list.Add(_testStr);
}
return string.Concat(list.ToArray());
}
}
 Stopwatch watch = new Stopwatch();
StringSpeedTest strSpeedTest = new StringSpeedTest(); int count = ;
TimeSpan time = new TimeSpan(, , );
watch.Start();
for (int i = ; i < ; i++)
{
Console.WriteLine("-------------------------------------------------------------------");
strSpeedTest.StringAdd(count); Console.WriteLine($"StringAdd:\t\t\t{watch.Elapsed - time}");
time = watch.Elapsed; strSpeedTest.UseStringBuilder(count);
Console.WriteLine($"UseStringBuilder:\t\t{watch.Elapsed - time}");
time = watch.Elapsed; strSpeedTest.UseStringBuilderWithCapacity(count);
Console.WriteLine($"UseStringBuilderWithCapacity:\t{watch.Elapsed - time}");
time = watch.Elapsed; strSpeedTest.UseStringConcatWithList(count);
Console.WriteLine($"UseStringConcatWithList:\t{watch.Elapsed - time}");
time = watch.Elapsed; strSpeedTest.UseStringConcatWithListToArray(count);
Console.WriteLine($"UseStringConcatWithListToArray:\t{watch.Elapsed - time}");
time = watch.Elapsed; strSpeedTest.UseStringConcat(count);
Console.WriteLine($"UseStringConcat:\t\t{watch.Elapsed - time}"); Console.WriteLine("*******************************************************************");
} watch.Stop();

测试结果:

 -------------------------------------------------------------------
StringAdd: ::00.2908582
UseStringBuilder: ::00.0008419
UseStringBuilderWithCapacity: ::00.0008429
UseStringConcatWithList: ::00.0101701
UseStringConcatWithListToArray: ::00.0032169
UseStringConcat: ::00.0005971
*******************************************************************
-------------------------------------------------------------------
StringAdd: ::00.1373059
UseStringBuilder: ::00.0003654
UseStringBuilderWithCapacity: ::00.0003533
UseStringConcatWithList: ::00.0006684
UseStringConcatWithListToArray: ::00.0003922
UseStringConcat: ::00.0003371
*******************************************************************
-------------------------------------------------------------------
StringAdd: ::00.1313924
UseStringBuilder: ::00.0003778
UseStringBuilderWithCapacity: ::00.0010882
UseStringConcatWithList: ::00.0010397
UseStringConcatWithListToArray: ::00.0007571
UseStringConcat: ::00.0004528
*******************************************************************
-------------------------------------------------------------------
StringAdd: ::00.1282163
UseStringBuilder: ::00.0003770
UseStringBuilderWithCapacity: ::00.0003452
UseStringConcatWithList: ::00.0006388
UseStringConcatWithListToArray: ::00.0003873
UseStringConcat: ::00.0004068
*******************************************************************
-------------------------------------------------------------------
StringAdd: ::00.1349978
UseStringBuilder: ::00.0004084
UseStringBuilderWithCapacity: ::00.0003698
UseStringConcatWithList: ::00.0006802
UseStringConcatWithListToArray: ::00.0004059
UseStringConcat: ::00.0003503
*******************************************************************
-------------------------------------------------------------------
StringAdd: ::00.1290642
UseStringBuilder: ::00.0003813
UseStringBuilderWithCapacity: ::00.0003674
UseStringConcatWithList: ::00.0006886
UseStringConcatWithListToArray: ::00.0004115
UseStringConcat: ::00.0003509
*******************************************************************
-------------------------------------------------------------------
StringAdd: ::00.1363654
UseStringBuilder: ::00.0003885
UseStringBuilderWithCapacity: ::00.0004270
UseStringConcatWithList: ::00.0007209
UseStringConcatWithListToArray: ::00.0004618
UseStringConcat: ::00.0003863
*******************************************************************
-------------------------------------------------------------------
StringAdd: ::00.1326732
UseStringBuilder: ::00.0003978
UseStringBuilderWithCapacity: ::00.0003965
UseStringConcatWithList: ::00.0006867
UseStringConcatWithListToArray: ::00.0004012
UseStringConcat: ::00.0003480
*******************************************************************
-------------------------------------------------------------------
StringAdd: ::00.1275684
UseStringBuilder: ::00.0003673
UseStringBuilderWithCapacity: ::00.0004190
UseStringConcatWithList: ::00.0007201
UseStringConcatWithListToArray: ::00.0004572
UseStringConcat: ::00.0003848
*******************************************************************
-------------------------------------------------------------------
StringAdd: ::00.1606576
UseStringBuilder: ::00.0004435
UseStringBuilderWithCapacity: ::00.0005415
UseStringConcatWithList: ::00.0007263
UseStringConcatWithListToArray: ::00.0004205
UseStringConcat: ::00.0003707
*******************************************************************

从结果中可以看到,StringBuilder 速度不是永远最快的。当把cout调整为100000时 结果如下,由于string直接+速度太慢直接去除比较梯队。

 -------------------------------------------------------------------
UseStringBuilder: ::00.1400624
UseStringBuilderWithCapacity: ::00.0057860
UseStringConcatWithList: ::00.0186478
UseStringConcatWithListToArray: ::00.0074943
UseStringConcat: ::00.0054871
*******************************************************************
-------------------------------------------------------------------
UseStringBuilder: ::00.0115386
UseStringBuilderWithCapacity: ::00.0057336
UseStringConcatWithList: ::00.0080408
UseStringConcatWithListToArray: ::00.0052401
UseStringConcat: ::00.0037864
*******************************************************************
-------------------------------------------------------------------
UseStringBuilder: ::00.0085633
UseStringBuilderWithCapacity: ::00.0049141
UseStringConcatWithList: ::00.0179742
UseStringConcatWithListToArray: ::00.0045335
UseStringConcat: ::00.0042053
*******************************************************************
-------------------------------------------------------------------
UseStringBuilder: ::00.0085083
UseStringBuilderWithCapacity: ::00.0041245
UseStringConcatWithList: ::00.0080081
UseStringConcatWithListToArray: ::00.0043282
UseStringConcat: ::00.0037264
*******************************************************************
-------------------------------------------------------------------
UseStringBuilder: ::00.0087733
UseStringBuilderWithCapacity: ::00.0086311
UseStringConcatWithList: ::00.0080520
UseStringConcatWithListToArray: ::00.0056323
UseStringConcat: ::00.0038094
*******************************************************************
-------------------------------------------------------------------
UseStringBuilder: ::00.0086884
UseStringBuilderWithCapacity: ::00.0063233
UseStringConcatWithList: ::00.0081824
UseStringConcatWithListToArray: ::00.0055393
UseStringConcat: ::00.0040967
*******************************************************************
-------------------------------------------------------------------
UseStringBuilder: ::00.0108527
UseStringBuilderWithCapacity: ::00.0048482
UseStringConcatWithList: ::00.0079176
UseStringConcatWithListToArray: ::00.0043536
UseStringConcat: ::00.0042921
*******************************************************************
-------------------------------------------------------------------
UseStringBuilder: ::00.0119569
UseStringBuilderWithCapacity: ::00.0043067
UseStringConcatWithList: ::00.0091552
UseStringConcatWithListToArray: ::00.0045956
UseStringConcat: ::00.0041058
*******************************************************************
-------------------------------------------------------------------
UseStringBuilder: ::00.0096342
UseStringBuilderWithCapacity: ::00.0055840
UseStringConcatWithList: ::00.0073000
UseStringConcatWithListToArray: ::00.0056438
UseStringConcat: ::00.0038800
*******************************************************************
-------------------------------------------------------------------
UseStringBuilder: ::00.0090576
UseStringBuilderWithCapacity: ::00.0047266
UseStringConcatWithList: ::00.0076383
UseStringConcatWithListToArray: ::00.0048706
UseStringConcat: ::00.0048964
*******************************************************************

从结果中可以看到再没有使用初始化StringBuilder 容器容量的时候,性能与使用string.Concat相差已经很大。在使用初始化容量之后 性能有较大的提升。

总结如下:

1、在只知道字符串最终长度的时候,可以使用 StringBuilder 并初始化容量,可以有较大的提升;

2、在不知道最终字符串长度时候,但是知道拼接字符串次数的时候,可以将每次字符串保存在一个字符串数组中,可以有较大的性能提升;

3、在不知道字符串长度以及拼接的次数的时候,可以使用LIst<string>  保存,可以有较大的性能提升。当然也可以大约估计最终字符串长度,然后初始化StringBuilder 时候使用较大的容量,以空间来换取速度

当然这些都是在拼接字符串的次数很大的情况下,会有较大的性能提升。

参考:

http://blog.zhaojie.me/2009/11/string-concat-perf-1-benchmark.html

StringBuilder String string.Concat 字符串拼接速度再议的更多相关文章

  1. StringBuilder String string.Concat 字符串拼接速度

    首先看测试代码: public class StringSpeedTest { "; public string StringAdd(int count) { string str = st ...

  2. js扩展String.prototype.format字符串拼接的功能

    1.题外话,有关概念理解:String.prototype 属性表示 String原型对象.所有 String 的实例都继承自 String.prototype. 任何String.prototype ...

  3. 从源代码的角度聊聊java中StringBuffer、StringBuilder、String中的字符串拼接

    长久以来,我们被教导字符串的连接最好用StringBuffer.StringBuilder,但是我们却不知道这两者之间的区别.跟字符串相关的一些方法中总是有CharSequence.StringBuf ...

  4. 将String类型的字符串拼接成以逗号分隔的字符输出

    package test; import java.util.ArrayList; import java.util.List; public class Tesdssss { private sta ...

  5. mysql 存储过程 CONCAT 字符串拼接

    mysql 存储过程 CREATE PROCEDURE pro_province_report (IN startDate VARCHAR(),IN endDate VARCHAR(),IN Sour ...

  6. Java中测试StringBuilder、StringBuffer、String在字符串拼接上的性能

    应一个大量字符串拼接的任务 测试一下StringBuilder.StringBuffer.String在操作字符串拼接时候的性能 性能上理论是StringBuilder  >  StringBu ...

  7. 从为什么String=String谈到StringBuilder和StringBuffer

    前言 有这么一段代码: public class TestMain { public static void main(String[] args) { String str0 = "123 ...

  8. StringBuilder与String互转

    StringBuilder类是一个可变的字符序列. StringBuilder()           构造一个不带任何字符的字符串生成器,其初始容量为 16 个字符.StringBuilder(Ch ...

  9. 字符串拼接原理 javac 和 javap

    一.新建一个 Test1 类 public class Test1 { public static void main(String[] args) { String a = "a" ...

随机推荐

  1. 扫描Linux服务器查找恶意软件和rootkit的一款工具

    官网参考官网安装教程:wget https://www.clamav.net/downloads/production/clamav-0.101.1.tar.gztar -zxvf clamav-0. ...

  2. jmeter(十一)JDBC Request之Query Type

    工作中遇到这样一个问题: 需要准备10W条测试数据,利用jmeter中的JDBC Request向数据库中批量插入这些数据(只要主键不重复就可以,利用函数助手中的Random将主键的ID末尾五位数随机 ...

  3. apache、nginx的虚拟域名配置和rewrite配置,以及web缓存的几种方式

    web缓存一般用来缓解数据库压力. 通常有几种方法,文件静态化,缓存服务memcached.redis等. 伪静态,一般指在url上貌似访问静态html页的形式,这样有利于搜索引擎访问到网站页面,实际 ...

  4. vba报表制作

    Option Explicit Dim sql, tj As String, rnum As Double, r As Integer  Private Sub CommandButton1_Clic ...

  5. HNOI2017做题笔记

    HNOI2017 单旋(线段树.set) 手玩旋转操作(忽略手玩过程)可以发现:一次单旋对原树的变化实际上很小. 对于父子关系,单旋最小值会将\(Spaly\)上最小值变成原来根的父亲,将最小值的点右 ...

  6. Luogu3959 NOIP2017 宝藏 状压DP

    题目传送门:https://www.luogu.org/problemnew/show/P3959 题意:给出一个有$N$个点的图,求其中的一个生成树(指定一个点为根),使得$\sum\limits_ ...

  7. Cordova套网站

    用Cordova套网站,只修改Content的话,打包后的App,在点击后会打开浏览器,并没有在App中显示内容. 需要设置allow-navigation为 * <?xml version=' ...

  8. WPF C#截图功能 仿qq截图

    原文:WPF C#截图功能 仿qq截图 先上效果图 源码下载地址:http://download.csdn.net/detail/candyvoice/9788099 描述:启动程序,点击窗口butt ...

  9. NoSql的三大基石:CAP理论&BASE&最终一致性

    关系型数据库的局限 NoSql出现在关系型数据库之后,主要是为了解决关系型数据库的短板,我们先来看看随着软件行业的发展,关系型数据库面临了哪些挑战: 1.高并发 一个最典型的就是电商网站,例如双11, ...

  10. [Oracle]如何取Control File 的Dump

    ]如何取Control File 的Dump: SQL> alter session set events 'immediate trace name controlf level 3';SQL ...