字符串拼接速度再议,减少垃圾回收压力的字符
分类:新闻中心

StringBuilder String string.Concat 字符串拼接速度再议,stringbuilder拼接sql

首先看测试代码:

图片 1

 1   public class StringSpeedTest
 2     {
 3         private readonly static string _testStr = "0123456789";
 4 
 5         public string StringAdd(int count)
 6         {
 7             string str = string.Empty;
 8             for (int i = 0; i < count; i++)
 9             {
10                 str += _testStr;
11             }
12             return str;
13         }
14 
15         public string UseStringBuilder(int count)
16         {
17             StringBuilder sb = new StringBuilder();
18 
19             for (int i = 0; i < count; i++)
20             {
21                 sb.Append(_testStr);
22             }
23 
24             return sb.ToString();
25         }
26         public string UseStringBuilderWithCapacity(int count)
27         {
28             StringBuilder sb = new StringBuilder(count * _testStr.Length);
29 
30             for (int i = 0; i < count; i++)
31             {
32                 sb.Append(_testStr);
33             }
34 
35             return sb.ToString();
36         }
37         public string UseStringConcat(int count)
38         {
39             string[] list = new string[count];
40             for (int i = 0; i < count; i++)
41             {
42                 list[i] = _testStr;
43             }
44             return string.Concat(list);
45         }
46         public string UseStringConcatWithList(int count)
47         {
48             List<string> list = new List<string>(count);
49             for (int i = 0; i < count; i++)
50             {
51                 list.Add(_testStr);
52             }
53             return string.Concat(list);
54         }
55         public string UseStringConcatWithListToArray(int count)
56         {
57             List<string> list = new List<string>(count);
58             for (int i = 0; i < count; i++)
59             {
60                 list.Add(_testStr);
61             }
62             return string.Concat(list.ToArray());
63         }
64     }

View Code 图片 2

 1 Stopwatch watch = new Stopwatch();
 2             StringSpeedTest strSpeedTest = new StringSpeedTest();
 3 
 4             int count = 10000;
 5             TimeSpan time = new TimeSpan(0, 0, 0);
 6             watch.Start();
 7             for (int i = 0; i < 10; i++)
 8             {
 9                 Console.WriteLine("-------------------------------------------------------------------");
10                 strSpeedTest.StringAdd(count);
11 
12                 Console.WriteLine($"StringAdd:ttt{watch.Elapsed - time}");
13                 time = watch.Elapsed;
14 
15                 strSpeedTest.UseStringBuilder(count);
16                 Console.WriteLine($"UseStringBuilder:tt{watch.Elapsed - time}");
17                 time = watch.Elapsed;
18 
19                 strSpeedTest.UseStringBuilderWithCapacity(count);
20                 Console.WriteLine($"UseStringBuilderWithCapacity:t{watch.Elapsed - time}");
21                 time = watch.Elapsed;
22 
23                 strSpeedTest.UseStringConcatWithList(count);
24                 Console.WriteLine($"UseStringConcatWithList:t{watch.Elapsed - time}");
25                 time = watch.Elapsed;
26 
27                 strSpeedTest.UseStringConcatWithListToArray(count);
28                 Console.WriteLine($"UseStringConcatWithListToArray:t{watch.Elapsed - time}");
29                 time = watch.Elapsed;
30 
31                 strSpeedTest.UseStringConcat(count);
32                 Console.WriteLine($"UseStringConcat:tt{watch.Elapsed - time}");
33 
34 
35                 Console.WriteLine("*******************************************************************");
36             }
37 
38             watch.Stop();

View Code

测试结果:

图片 3

 1 -------------------------------------------------------------------
 2 StringAdd:                      00:00:00.2908582
 3 UseStringBuilder:               00:00:00.0008419
 4 UseStringBuilderWithCapacity:   00:00:00.0008429
 5 UseStringConcatWithList:        00:00:00.0101701
 6 UseStringConcatWithListToArray: 00:00:00.0032169
 7 UseStringConcat:                00:00:00.0005971
 8 *******************************************************************
 9 -------------------------------------------------------------------
10 StringAdd:                      00:00:00.1373059
11 UseStringBuilder:               00:00:00.0003654
12 UseStringBuilderWithCapacity:   00:00:00.0003533
13 UseStringConcatWithList:        00:00:00.0006684
14 UseStringConcatWithListToArray: 00:00:00.0003922
15 UseStringConcat:                00:00:00.0003371
16 *******************************************************************
17 -------------------------------------------------------------------
18 StringAdd:                      00:00:00.1313924
19 UseStringBuilder:               00:00:00.0003778
20 UseStringBuilderWithCapacity:   00:00:00.0010882
21 UseStringConcatWithList:        00:00:00.0010397
22 UseStringConcatWithListToArray: 00:00:00.0007571
23 UseStringConcat:                00:00:00.0004528
24 *******************************************************************
25 -------------------------------------------------------------------
26 StringAdd:                      00:00:00.1282163
27 UseStringBuilder:               00:00:00.0003770
28 UseStringBuilderWithCapacity:   00:00:00.0003452
29 UseStringConcatWithList:        00:00:00.0006388
30 UseStringConcatWithListToArray: 00:00:00.0003873
31 UseStringConcat:                00:00:00.0004068
32 *******************************************************************
33 -------------------------------------------------------------------
34 StringAdd:                      00:00:00.1349978
35 UseStringBuilder:               00:00:00.0004084
36 UseStringBuilderWithCapacity:   00:00:00.0003698
37 UseStringConcatWithList:        00:00:00.0006802
38 UseStringConcatWithListToArray: 00:00:00.0004059
39 UseStringConcat:                00:00:00.0003503
40 *******************************************************************
41 -------------------------------------------------------------------
42 StringAdd:                      00:00:00.1290642
43 UseStringBuilder:               00:00:00.0003813
44 UseStringBuilderWithCapacity:   00:00:00.0003674
45 UseStringConcatWithList:        00:00:00.0006886
46 UseStringConcatWithListToArray: 00:00:00.0004115
47 UseStringConcat:                00:00:00.0003509
48 *******************************************************************
49 -------------------------------------------------------------------
50 StringAdd:                      00:00:00.1363654
51 UseStringBuilder:               00:00:00.0003885
52 UseStringBuilderWithCapacity:   00:00:00.0004270
53 UseStringConcatWithList:        00:00:00.0007209
54 UseStringConcatWithListToArray: 00:00:00.0004618
55 UseStringConcat:                00:00:00.0003863
56 *******************************************************************
57 -------------------------------------------------------------------
58 StringAdd:                      00:00:00.1326732
59 UseStringBuilder:               00:00:00.0003978
60 UseStringBuilderWithCapacity:   00:00:00.0003965
61 UseStringConcatWithList:        00:00:00.0006867
62 UseStringConcatWithListToArray: 00:00:00.0004012
63 UseStringConcat:                00:00:00.0003480
64 *******************************************************************
65 -------------------------------------------------------------------
66 StringAdd:                      00:00:00.1275684
67 UseStringBuilder:               00:00:00.0003673
68 UseStringBuilderWithCapacity:   00:00:00.0004190
69 UseStringConcatWithList:        00:00:00.0007201
70 UseStringConcatWithListToArray: 00:00:00.0004572
71 UseStringConcat:                00:00:00.0003848
72 *******************************************************************
73 -------------------------------------------------------------------
74 StringAdd:                      00:00:00.1606576
75 UseStringBuilder:               00:00:00.0004435
76 UseStringBuilderWithCapacity:   00:00:00.0005415
77 UseStringConcatWithList:        00:00:00.0007263
78 UseStringConcatWithListToArray: 00:00:00.0004205
79 UseStringConcat:                00:00:00.0003707
80 *******************************************************************

View Code

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

图片 4

 1 -------------------------------------------------------------------
 2 UseStringBuilder:               00:00:00.1400624
 3 UseStringBuilderWithCapacity:   00:00:00.0057860
 4 UseStringConcatWithList:        00:00:00.0186478
 5 UseStringConcatWithListToArray: 00:00:00.0074943
 6 UseStringConcat:                00:00:00.0054871
 7 *******************************************************************
 8 -------------------------------------------------------------------
 9 UseStringBuilder:               00:00:00.0115386
10 UseStringBuilderWithCapacity:   00:00:00.0057336
11 UseStringConcatWithList:        00:00:00.0080408
12 UseStringConcatWithListToArray: 00:00:00.0052401
13 UseStringConcat:                00:00:00.0037864
14 *******************************************************************
15 -------------------------------------------------------------------
16 UseStringBuilder:               00:00:00.0085633
17 UseStringBuilderWithCapacity:   00:00:00.0049141
18 UseStringConcatWithList:        00:00:00.0179742
19 UseStringConcatWithListToArray: 00:00:00.0045335
20 UseStringConcat:                00:00:00.0042053
21 *******************************************************************
22 -------------------------------------------------------------------
23 UseStringBuilder:               00:00:00.0085083
24 UseStringBuilderWithCapacity:   00:00:00.0041245
25 UseStringConcatWithList:        00:00:00.0080081
26 UseStringConcatWithListToArray: 00:00:00.0043282
27 UseStringConcat:                00:00:00.0037264
28 *******************************************************************
29 -------------------------------------------------------------------
30 UseStringBuilder:               00:00:00.0087733
31 UseStringBuilderWithCapacity:   00:00:00.0086311
32 UseStringConcatWithList:        00:00:00.0080520
33 UseStringConcatWithListToArray: 00:00:00.0056323
34 UseStringConcat:                00:00:00.0038094
35 *******************************************************************
36 -------------------------------------------------------------------
37 UseStringBuilder:               00:00:00.0086884
38 UseStringBuilderWithCapacity:   00:00:00.0063233
39 UseStringConcatWithList:        00:00:00.0081824
40 UseStringConcatWithListToArray: 00:00:00.0055393
41 UseStringConcat:                00:00:00.0040967
42 *******************************************************************
43 -------------------------------------------------------------------
44 UseStringBuilder:               00:00:00.0108527
45 UseStringBuilderWithCapacity:   00:00:00.0048482
46 UseStringConcatWithList:        00:00:00.0079176
47 UseStringConcatWithListToArray: 00:00:00.0043536
48 UseStringConcat:                00:00:00.0042921
49 *******************************************************************
50 -------------------------------------------------------------------
51 UseStringBuilder:               00:00:00.0119569
52 UseStringBuilderWithCapacity:   00:00:00.0043067
53 UseStringConcatWithList:        00:00:00.0091552
54 UseStringConcatWithListToArray: 00:00:00.0045956
55 UseStringConcat:                00:00:00.0041058
56 *******************************************************************
57 -------------------------------------------------------------------
58 UseStringBuilder:               00:00:00.0096342
59 UseStringBuilderWithCapacity:   00:00:00.0055840
60 UseStringConcatWithList:        00:00:00.0073000
61 UseStringConcatWithListToArray: 00:00:00.0056438
62 UseStringConcat:                00:00:00.0038800
63 *******************************************************************
64 -------------------------------------------------------------------
65 UseStringBuilder:               00:00:00.0090576
66 UseStringBuilderWithCapacity:   00:00:00.0047266
67 UseStringConcatWithList:        00:00:00.0076383
68 UseStringConcatWithListToArray: 00:00:00.0048706
69 UseStringConcat:                00:00:00.0048964
70 *******************************************************************

View Code

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

总结如下:

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

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

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

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

 

参考:

String string.Concat 字符串拼接速度再议,stringbuilder拼接sql 首先看测试代码: 1 public class StringSpeedTest 2 { 3 private readonly static string...

StringBuilder sb = new StringBuilder(); 
for (int i = 0; i < 10; i++) 
{ 
 sb.Append(i); 
} 
string s = sb.ToString(); 
//建议修改为 
StringBuilder sb = new StringBuilder(256); 
for (int i = 0; i < 10; i++) 
{ 
 sb.Append(i); 
} 
string s = sb.ToString(); 
StringBuiler sb = new StringBuilder(256); 
 ...... 
sb.Remove(0, sb.Length); //不建议 
sb.Length = 0; //建议 

8、不要使用.Length=0来释放StringBuilder占用的内存

5、创建StringBuilder应指定初始大小

  • 类似str1+str2+str3+str4的字符串拼接操作会被编译为 String.Concat(str1,str2,str3, str4),效率反而高于StringBuilder。String.Concat会一次性确定字符串长度, StringBuilder需要做Resize,适用于多次生成string对象的情况。
//不建议: 
string s = null; 
for (int i = 0; i < 10000; i++) 
{ 
s += i; 
} 
//建议: 
StringBuilder sb = new StringBuilder(); 
for (int i = 0; i < 10000; i++) 
{ 
sb.Append(i); 
} 
string t = sb.ToString();
  • 默认的初始大小为16,一旦超过即需要Resize一次并增加GC压力。建议根据经验值为其指定初始大小。

C# 性能优化细节

4、巧用StringBuilder进行字符串拼接操作

  • 根据实验结果,多次使用同一StringBuilder对象时,通过直接设置.Length=0来初始化速度最快。

2、使用str.Length == 0做空串比较

7、通过直接设置.Length=0来初始化StringBuilder

  • 最快的方法:if (str.Length == 0)
  • 其次:if (str == String.Empty)或 if (str == "")

1、使用string.Empty给一个空字符串变量赋初始值

  • ToUpper、ToLower这类方法均会重新生成字符串对
  • String.Compare可实现忽略字符串大小写

6、避免滥用StringBuilder

  • String.Empty是一个指代,而””是具体的实现

3、避免不必要的字符串ToUpper、ToLower类操作

static void test() 
{ 
 StringBuilder sb = new StringBuilder(256); 
 for (int i = 0; i < 100; i++) 
 { 
  sb.Append(i); 
 } 
 string t = sb.ToString(); 
 ……//其他不使用变量sb的代码段 
 sb.Length = 0; //去掉该句手工清空sb代码,会更早释放内存 
} 

您可能感兴趣的文章:

  • 非常实用的C#字符串操作处理类StringHelper.cs
  • 总结的5个C#字符串操作方法分享
  • C#自定义的字符串操作增强类实例
  • c#对字符串操作的技巧小结
  • C#中一些字符串操作的常用用法

图片 5

string filter=“”;//不建议 string filter=string.Empty; //建议
  • 如果要构造一个较长的字符串,尤其是拼接超过10次时(经验值),应使用StringBuilder做字符串拼接操作。
//不推荐的写法if(s1.ToUpper()==s2.ToUpper()) …;//推荐的写法if(String.Compare( s1, s2, true ) == 0) …;

本文由美高梅网址发布于新闻中心,转载请注明出处:字符串拼接速度再议,减少垃圾回收压力的字符

上一篇:没有了 下一篇:没有了
猜你喜欢
热门排行
精彩图文