C# string与StringBuilder速度测试
测试代码
Stopwatch time1 = new Stopwatch(); Stopwatch time2 = new Stopwatch(); string str = String.Empty; StringBuilder sb = new StringBuilder(); time1 .Start (); for (int i = 0; i < 100000; i++) { str+=i.ToString(); } time1.Stop (); Console.WriteLine(time1.Elapsed); time2 .Start (); for (int i = 0; i < 100000; i++) { sb.Append(i.ToString()); } time2.Stop(); Console.WriteLine(time2.Elapsed); Console.ReadKey();
输出
00:00:13.2234835 00:00:00.0073885
转载请注明出处。