[C#]字串組合的時間效能大比拚(+=、Str.Format、StringBuilder.Append、SB.AppendFormat)

[C#]字串組合的時間效能大比拚

 

使用情境:

程式常常會需要組合字串,尤其SQL語法上最常使用,

其實我都是用String.Format,因為程式只要一行,且分析簡單

不過最近想評估看看普遍看到的 +=、自己常用的Str.FormatStringBuilder的效能比較

評估後,StringBuilder效能真的比較高,而且類別還是有AppendFormat

雖然此Method的效能也較慢(一億次),也算是日後改進自己寫程式的方法吧~

測試系統:

Lenovo-X200,WinXP SP3,記憶體裝到4G,開發工具VS2008

程式碼:


        {
            string sResult = "";
            int iCount = Convert.ToInt32(TB_Count.Text);
            iCount = iCount >> 1;

            sResult += "測試次數:"+TB_Count.Text+"\r\n";

            //=======  +=
            Stopwatch sw = new Stopwatch();
            sw.Reset();
            sw = Stopwatch.StartNew();
            string sTemp1 = "";
            for (int iiiii = 0; iiiii < iCount; iiiii++)
            {
                sTemp1 += "a";
            }
            sw.Stop();
            long ms = sw.ElapsedMilliseconds;
            sResult += string.Format("+=方式時間: {0} ms\r\n", ms);
            Thread.Sleep(1000);

            ////=======String.Format();
            sw.Reset();
            sw = Stopwatch.StartNew();
            string sTemp2 = "";
            for (int iiiii = 0; iiiii < iCount; iiiii++)
            {
                sTemp2 = String.Format("{0}a", sTemp1);
            }
            sw.Stop();
            ms = sw.ElapsedMilliseconds;
            sResult += string.Format("Str.Format()方式時間: {0} ms\r\n", ms);
            Thread.Sleep(1000);

            //=======StringBuilder;
            sw.Reset();
            sw = Stopwatch.StartNew();
            StringBuilder sb1 = new StringBuilder();
            for (int iiiii = 0; iiiii < iCount; iiiii++)
            {
                sb1.Append("a");
            }
            sw.Stop();
            ms = sw.ElapsedMilliseconds;
            sResult += string.Format("SB.Append方式時間: {0} ms\r\n", ms);
            Thread.Sleep(1000);

            //=======StringBuilder;
            sw.Reset();
            sw = Stopwatch.StartNew();
            StringBuilder sb2 = new StringBuilder();
            for (int iiiii = 0; iiiii < iCount; iiiii++)
            {
                sb2.AppendFormat("{0}", "a");
            }
            sw.Stop();
            ms = sw.ElapsedMilliseconds;
            sResult += string.Format("SB.AppendFormat方式時間: {0} ms\r\n", ms);
            Thread.Sleep(1000);

            //=======StringBuilder;
            sw.Reset();
            sw = Stopwatch.StartNew();
            StringBuilder sb3 = new StringBuilder(iCount);
            for (int iiiii = 0; iiiii < iCount; iiiii++)
            {
                sb3.Append("a");
            }
            sw.Stop();
            ms = sw.ElapsedMilliseconds;
            sResult += string.Format("SB.Append預設長度時間: {0} ms\r\n", ms);
            Thread.Sleep(1000);

            //=======StringBuilder;
            sw.Reset();
            sw = Stopwatch.StartNew();
            StringBuilder sb4 = new StringBuilder(iCount >> 1);
            for (int iiiii = 0; iiiii < iCount; iiiii++)
            {
                sb4.Append("a");
            }
            sw.Stop();
            ms = sw.ElapsedMilliseconds;
            sResult += string.Format("SB.Append預設1/2長度時間: {0} ms\r\n", ms);
            Thread.Sleep(1000);

            MessageBox.Show(sResult, "Process End");
        }

效能比較表: