您的位置:首页 > 编程语言 > C#

C#性能之字符串拼接

2013-06-10 13:16 204 查看
在平时代码中,字符串的拼接方式有采用 + 和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("|");
这样类似的方式,因为这种方式耗时少,并且内存消耗也比较少。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: