字符串连接符效率分析

来源:互联网 发布:js 中文简体繁体转换 编辑:程序博客网 时间:2024/05/21 07:11
Stopwatch st1 = new Stopwatch();            Stopwatch st2 = new Stopwatch();            Stopwatch st3 = new Stopwatch();            st1.Start();             for (int i = 0; i < 10000000; i++)            {                string a = "1234567";                string b = "abcdefg";                string c = a + b;            }            st1.Stop();            st2.Start();            for (int i = 0; i < 10000000; i++)            {                string a = "1234567";                string b = "abcdefg";                string c = string.Format("{0}{1}", a, b);            }            st2.Stop();            st3.Start();            for (int i = 0; i < 10000000; i++)            {                string a = "1234567";                string b = "abcdefg";                StringBuilder c = new StringBuilder();                c.Append(a);                c.Append(b);            }            st3.Stop();            Console.WriteLine("st1:{0}", st1.ElapsedMilliseconds);            Console.WriteLine("st2:{0}", st2.ElapsedMilliseconds);            Console.WriteLine("st3:{0}", st3.ElapsedMilliseconds);结果:st1:570st2:1690st3:563
原创粉丝点击