日期:2014-05-18 浏览次数:20780 次
class Program { static void Main(string[] args) { var a = Guid.NewGuid().ToString(); var b = Guid.NewGuid().ToString(); var c = Guid.NewGuid().ToString(); var sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 1000000; i++) { var s = "123" + a + "456" + b + "789" + c; } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); sw.Restart(); for (int i = 0; i < 1000000; i++) { var s = String.Format("123{0}456{1}789{2}", a, b, c); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); } }
static void Main(string[] args) { var a = Guid.NewGuid().ToString(); var b = Guid.NewGuid().ToString(); var c = Guid.NewGuid().ToString(); var str1 = string.Empty; var str2 = string.Empty; var sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 10000; i++) { str1 += string.Format("123{0}456{1}789{2}", a, b, c); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10000; i++) { sb.AppendFormat("123{0}456{1}789{2}", a, b, c); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < 10000; i++) { str2 += "123" + a + "456" + b + "789" + c; } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); Console.ReadLine(); } //测试最后的字符串长度都是1170000 //1.string.format用时:15973 //2.AppendFormat用时:18 //3.+用时:16814 //追加字符串时2的速度很明显,而2和3速度都快不到哪去,两者不相上下
------解决方案--------------------
http://www.codeproject.com/Articles/3377/Strings-UNDOCUMENTED#xx367414xx
------解决方案--------------------