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

Posted 浅苍蓝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了StringBuilder String string.Concat 字符串拼接速度相关的知识,希望对你有一定的参考价值。

首先看测试代码:

public class StringSpeedTest
    {
        private readonly static string _testStr = "0123456789";

        public string StringAdd(int count)
        {
            string str = string.Empty;
            for (int i = 0; i < count; i++)
            {
                str += _testStr;
            }
            return str;
        }

        public string UseStringBuilder(int count)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < count; i++)
            {
                sb.Append(_testStr);
            }

            return sb.ToString();
        }
        public string UseStringBuilderWithCapacity(int count)
        {
            StringBuilder sb = new StringBuilder(count * _testStr.Length);

            for (int i = 0; i < count; i++)
            {
                sb.Append(_testStr);
            }

            return sb.ToString();
        }
        public string UseStringConcat(int count)
        {
            string[] list = new string[count];
            for (int i = 0; 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 = 0; 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 = 0; i < count; i++)
            {
                list.Add(_testStr);
            }
            return string.Concat(list.ToArray());
        }
    }
View Code
Stopwatch watch = new Stopwatch();
            StringSpeedTest strSpeedTest = new StringSpeedTest();

            int count = 10000;
            TimeSpan time = new TimeSpan(0, 0, 0);
            watch.Start();
            for (int i = 0; i < 10; 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();
View Code

测试结果:

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

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

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

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

总结如下:

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

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

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

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

以上是关于StringBuilder String string.Concat 字符串拼接速度的主要内容,如果未能解决你的问题,请参考以下文章

String与StringBuilder区别总结

String,StringBuffer与StringBuilder

String,StringBuffer与StringBuilder

Java String StringBuffer StringBuilder

String,StringBuffer与StringBuilder的区别

String,StringBuffer与StringBuilder的区别??