C#性能之字符串拼接

在平時代碼中,字符串的拼接方式有采用 + 和StringBuilder兩種方式。本文采用3中方式來測試這兩種拼接的效率差別。

首先提供簡單的測試方法:採用Stopwatch來測試執行的時間、GC.CollectionCount(0); GC.CollectionCount(1); GC.CollectionCount(2);從側面反映託管區內存的分配情況。

測試代碼如下:

運行環境爲.NET4.0

1、測試性能的方法:

 1  public static void TestMethodPerformance<T>(Action<T> action, T t, int times)
 2         {
 3             int startGc0 = GC.CollectionCount(0);
 4             int startGc1 = GC.CollectionCount(1);
 5             int startGc2 = GC.CollectionCount(2);
 6 
 7             Stopwatch stopwatch = Stopwatch.StartNew();
 8             for (int i = 0; i < times; i++)
 9             {
10                 action(t);
11             }
12             stopwatch.Stop();
13             int endGc0 = GC.CollectionCount(0);
14             int endGc1 = GC.CollectionCount(1);
15             int endGc2 = GC.CollectionCount(2);
16 
17             Console.WriteLine("耗時:" + stopwatch.ElapsedMilliseconds + " ms");
18             Console.WriteLine("第0代回收次數:" + (endGc0 - startGc0));
19             Console.WriteLine("第1代回收次數:" + (endGc1 - startGc1));
20             Console.WriteLine("第2代回收次數:" + (endGc2 - startGc2));
21         }
View Code

2、被測的方法:

 1 private static void Main(string[] args)
 2         {
 3             string temp = "TestTestTestTest";
 4 
 5             MethodTest.TestMethodPerformance(Sb1, temp, 10000);
 6             MethodTest.TestMethodPerformance(Sb2, temp, 10000);
 7             MethodTest.TestMethodPerformance(Sb3, temp, 10000);
 8             Console.ReadLine();
 9         }
10 
11         private static void Sb1(string temp)
12         {
13 
14             StringBuilder sb1 = new StringBuilder();
15             sb1.Append(temp + "|");
16             sb1.Append(temp + "|");
17             sb1.Append(temp + "|");
18             sb1.Append(temp + "|");
19             sb1.Append(temp + "|");
20             sb1.Append(temp + "|");
21             sb1.Append(temp + "|");
22             sb1.Append(temp + "|");
23             sb1.Append(temp + "|");
24             sb1.Append(temp);
25 
26         }
27 
28         private static void Sb2(string temp)
29         {
30             StringBuilder sb2 = new StringBuilder();
31             sb2.Append(temp);
32             sb2.Append("|");
33             sb2.Append(temp);
34             sb2.Append("|");
35             sb2.Append(temp);
36             sb2.Append("|");
37             sb2.Append(temp);
38             sb2.Append("|");
39             sb2.Append(temp);
40             sb2.Append("|");
41             sb2.Append(temp);
42             sb2.Append("|");
43             sb2.Append(temp);
44             sb2.Append("|");
45             sb2.Append(temp);
46             sb2.Append("|");
47             sb2.Append(temp);
48             sb2.Append("|");
49             sb2.Append(temp);
50         }
51 
52         private static void Sb3(string temp)
53         {
54 
55             string sb3 = temp + "|" + temp + "|" + temp + "|" + temp;
56             sb3 += "|" + temp + "|" + temp;
57             sb3 += "|" + temp + "|" + temp + "|";
58             sb3 += temp + "|" + temp;
59 
60         }
View Code

運行結果:

多次運行代碼,運行的結果可能會有所不同。

結論:

在拼接例如:AAA|AAA|AAA之類的字符串時,建議採用

StringBuilder sb=new StringBuilder();

sb.Append(“AAA”);

sb.Append("|");

這樣類似的方式,因爲這種方式耗時少,並且內存消耗也比較少。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章