C#性能之字符串拼接
2013-06-10 13:16
204 查看
在平时代码中,字符串的拼接方式有采用 + 和StringBuilder两种方式。本文采用3中方式来测试这两种拼接的效率差别。
首先提供简单的测试方法:采用Stopwatch来测试执行的时间、GC.CollectionCount(0); GC.CollectionCount(1); GC.CollectionCount(2);从侧面反映托管区内存的分配情况。
测试代码如下:
运行环境为.NET4.0
1、测试性能的方法:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.cnitblog.com/blog/249579/201305/27231356-80a60e45779b491da17bc0737f01330c.x-png)
多次运行代码,运行的结果可能会有所不同。
结论:
在拼接例如:AAA|AAA|AAA之类的字符串时,建议采用
StringBuilder sb=new StringBuilder();
sb.Append(“AAA”);
sb.Append("|");
这样类似的方式,因为这种方式耗时少,并且内存消耗也比较少。
首先提供简单的测试方法:采用Stopwatch来测试执行的时间、GC.CollectionCount(0); GC.CollectionCount(1); GC.CollectionCount(2);从侧面反映托管区内存的分配情况。
测试代码如下:
运行环境为.NET4.0
1、测试性能的方法:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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、被测的方法:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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("|");
这样类似的方式,因为这种方式耗时少,并且内存消耗也比较少。
相关文章推荐
- C#字符串连接和StringBuilder字符串拼接性能测试
- C# 性能优化——三种字符串拼接效率
- C#三种字符串拼接方法性能浅析
- C# 利用StringBuilder提升字符串拼接性能
- C# 拼接字符串的几种方式和性能
- C# 拼接字符串的几种方式和性能
- javascript模拟 C#中的StringBuilder,提升JS中字符串拼接的效率及性能
- C#性能之字符串拼接
- C# 利用StringBuilder提升字符串拼接性能的小例子
- C#和C++字符串拼接的性能分析
- Ruby 字符串拼接性能比较
- 探讨js字符串数组拼接的性能问题
- 字符串拼接性能优化
- 判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)--来源CSDN
- String,StringBuilder字符串拼接性能问题
- Java 5种字符串拼接方式性能比较。
- JAVA的字符串拼接与性能
- Java 5种字符串拼接方式性能比较。 .
- C# 三种字节数组(byte[])拼接的性能对比测试
- C# 三种字节数组(byte[])拼接的性能对比测试