C# string.format、string.connect和+=运算 效率计算

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringFormatEfficiency
{
    class Program
    {
        static void Main(string[] args)
        {
            string format = "my {0} is {1}";
            string name = "name";
            string zhangyaolin = "zhangyaolin";
            string my = "my ";
            string iss = " is ";
            DateTime  start = new DateTime();
            DateTime end = new DateTime();
            string aa = null;
           
            int maxcount = 10e6;

            for (int ii = 0; ii < 10; ii++)
            {
                start = DateTime.Now;

                for (int i = 0; i < maxcount; i++)
                {
                    aa = string.Format(format, name, zhangyaolin);
                }

                end = DateTime.Now;

                TimeSpan ts1 = end - start;

                Console.WriteLine(ts1.Milliseconds.ToString());

                //-------------------------

                start = DateTime.Now;

                for (int i = 0; i < maxcount; i++)
                {
                    aa = string.Concat(my, name, iss, zhangyaolin);
                }

                end = DateTime.Now;

                TimeSpan ts2 = end - start;

                Console.WriteLine(ts2.Milliseconds.ToString());

                //--------------------------

                start = DateTime.Now;

                for (int i = 0; i < maxcount; i++)
                {
                    aa = my + name + iss + zhangyaolin;
                }

                end = DateTime.Now;

                TimeSpan ts3 = end - start;

                Console.WriteLine(ts3.Milliseconds.ToString());

                Console.WriteLine("time1(format) : time2(connect) : time3(operator) =  {0} : {1} : 1", (1.0 * ts1.Milliseconds / ts3.Milliseconds).ToString(“F3”), (1.0 * ts2.Milliseconds / ts3.Milliseconds).ToString(“F3”));
                Console.WriteLine();

            }
        }
    }
}

测试结果:

531
109
125
time1(format) : time2(connect) : time3(operator) =  4.248 : 0.872 : 1
 
531
109
109
time1(format) : time2(connect) : time3(operator) =  4.872 : 1.000 : 1
 
515
125
109
time1(format) : time2(connect) : time3(operator) =  4.725 : 1.147 : 1
 
531
109
109
time1(format) : time2(connect) : time3(operator) =  4.872 : 1.000 : 1
 
531
125
109
time1(format) : time2(connect) : time3(operator) =  4.872 : 1.147 : 1
 
531
109
109
time1(format) : time2(connect) : time3(operator) =  4.872 : 1.000 : 1
 
531
109
109
time1(format) : time2(connect) : time3(operator) =  4.872 : 1.000 : 1
 
531
109
109
time1(format) : time2(connect) : time3(operator) =  4.872 : 1.000 : 1
 
531
125
109
time1(format) : time2(connect) : time3(operator) =  4.872 : 1.147 : 1
 
531
109
125
time1(format) : time2(connect) : time3(operator) =  4.248 : 0.872 : 1 
 
 
原因:
    string.format和string.connect都执行了新地址分配和入栈操作,不过string.format会对每个入栈字符检查判断,当有{\d+}这种替换字符出现时,会将{\d+}对应的字符序列入栈;而string.connect只做入栈,不做入栈字符的检查和判断。入栈操作完成时,做出栈操作,将栈内字符有序的放入堆里。理论上来说,string.format操作的字符串越长,效率越低。
注:\d+表示一个数字,如string format = "my {0} is {1}";中的0和1

 

posted @ 2014-08-08 20:43  牧师/preacher  阅读(1526)  评论(2编辑  收藏  举报