日期:2014-05-18  浏览次数:20780 次

为什么总看到有人说因为用了StringBuilder所以string.Format一定效率高
C# code


    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);
        }
    }







------解决方案--------------------
String.Format其实内部使用的就是StringBuilder


public static string Format(IFormatProvider provider, string format, params object[] args)
{
if ((format == null) || (args == null))
{
throw new ArgumentNullException((format == null) ? "format" : "args");
}
StringBuilder builder = new StringBuilder(format.Length + (args.Length * 8));
builder.AppendFormat(provider, format, args);
return builder.ToString();
}

 

------解决方案--------------------
你的循环体里面字符串都是一样的当然前者快 字符串驻留机制 试试后面+i
------解决方案--------------------
对于固定长度字符串运算,format没有优势,format优势在于,操作起来更加直观
处理变长度字符串 StringBuilder效率高,越长越高
------解决方案--------------------

测试一下追加字符串才比较有意义

C# code


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
------解决方案--------------------
探讨

我的时间 还算上了这句话:
StringBuilder sb = new StringBuilder();

------解决方案--------------------
原来楼主是说一次性拼接字符串啊。