首先看測試代碼: 1 public class StringSpeedTest 2 { 3 private readonly static string _testStr = "0123456789"; 4 5 public string StringAdd(int count) 6 { 7 str ...
首先看測試代碼:
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
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:\t\t\t{watch.Elapsed - time}"); 13 time = watch.Elapsed; 14 15 strSpeedTest.UseStringBuilder(count); 16 Console.WriteLine($"UseStringBuilder:\t\t{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:\t\t{watch.Elapsed - time}"); 33 34 35 Console.WriteLine("*******************************************************************"); 36 } 37 38 watch.Stop();View Code
測試結果:
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直接+速度太慢直接去除比較梯隊。
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 時候使用較大的容量,以空間來換取速度
當然這些都是在拼接字元串的次數很大的情況下,會有較大的性能提升。
參考:
http://blog.zhaojie.me/2009/11/string-concat-perf-1-benchmark.html