StringBuilder String string.Concat 字符串拼接速度
首先看测试代码:
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 时候使用较大的容量,以空间来换取速度
当然这些都是在拼接字符串的次数很大的情况下,会有较大的性能提升。
StringBuilder String string.Concat 字符串拼接速度的更多相关文章
- StringBuilder String string.Concat 字符串拼接速度再议
首先看测试代码: public class StringSpeedTest { "; public string StringAdd(int count) { string str = st ...
- js扩展String.prototype.format字符串拼接的功能
1.题外话,有关概念理解:String.prototype 属性表示 String原型对象.所有 String 的实例都继承自 String.prototype. 任何String.prototype ...
- 从源代码的角度聊聊java中StringBuffer、StringBuilder、String中的字符串拼接
长久以来,我们被教导字符串的连接最好用StringBuffer.StringBuilder,但是我们却不知道这两者之间的区别.跟字符串相关的一些方法中总是有CharSequence.StringBuf ...
- 将String类型的字符串拼接成以逗号分隔的字符输出
package test; import java.util.ArrayList; import java.util.List; public class Tesdssss { private sta ...
- mysql 存储过程 CONCAT 字符串拼接
mysql 存储过程 CREATE PROCEDURE pro_province_report (IN startDate VARCHAR(),IN endDate VARCHAR(),IN Sour ...
- Java中测试StringBuilder、StringBuffer、String在字符串拼接上的性能
应一个大量字符串拼接的任务 测试一下StringBuilder.StringBuffer.String在操作字符串拼接时候的性能 性能上理论是StringBuilder > StringBu ...
- 从为什么String=String谈到StringBuilder和StringBuffer
前言 有这么一段代码: public class TestMain { public static void main(String[] args) { String str0 = "123 ...
- StringBuilder与String互转
StringBuilder类是一个可变的字符序列. StringBuilder() 构造一个不带任何字符的字符串生成器,其初始容量为 16 个字符.StringBuilder(Ch ...
- 字符串拼接原理 javac 和 javap
一.新建一个 Test1 类 public class Test1 { public static void main(String[] args) { String a = "a" ...
随机推荐
- openssl之BIO系列之18---接受(accept)类型BIO
接受(accept)类型BIO ---依据openssl doc\crypto\bio_s_accept.pod翻译和自己的理解写成 (作者:DragonKing, Mail: wzhah@263.n ...
- anaconda指定镜像源,解决conda下载速度慢失败问题
conda升级默认官网地址,速度会特别慢,现在我们指定一个当前可用的镜像,步骤如下: 1.执行命令,生成.condarc文件 conda config --add channels https://m ...
- curl 远程下载图片
<?php echo httpcopy("http://www.baidu.com/img/baidu_sylogo1.gif"); function httpcopy($u ...
- uboot中断功能实现
uboot中实现irq中断(uboot version2015.04)1.实验目的:实现GPIO_2的外部中断 2.实验步骤:a.GPIO_2(GPIO1_IO02)为中断源, 首先需要设置这个pad ...
- 理解Callable 和 Spring DeferredResult(翻译)
1-介绍 Servlet 3中的异步支持为在另一个线程中处理HTTP请求提供了可能性.当有一个长时间运行的任务时,这是特别有趣的,因为当另一个线程处理这个请求时,容器线程被释放,并且可以继续为其他请求 ...
- 用node.js进行微信公众平台的开发
基本原理 用nodejs怎样来实现对微信公众平台的开发呢? 别的就不多说了,先来简单介绍微信公众平台的基本原理. 微信服务器就相当于一个转发服务器,终端(手机.Pad等)发起请求至微信服务器,微信服务 ...
- SWT将系统图标保存为本地文件
public class SWTImage { public static void main(String[] args) { final Display display = ...
- 动态添加js的方法
var Skip={};//获取XMLHttpRequest对象(提供客户端同http服务器通讯的协议)Skip.getXmlHttpRequest=function (){ if ( window. ...
- 我的直播demo
推流工具: ffmpeg 接收/转发流工具: nginx-rtmp 在线播放工具: video.js -------------------------------------- demo地址: ht ...
- 关于pom.xml中的MAVEN出错处理
原因:maven与pom的版本不一致 解决方法: 1.升级maven 插件: help--install..... 2.根据出错的信息,找到没有下载成功的本地仓库目录 ,将目录下的文件删除,再在项目上 ...