String+ String.Concat String.Format StringBuilder 之间的性能测试

找到一篇国外的代码,专门来测试这个,

String+

String.Concat

String.Format

StringBuilder

前三个在100个左右字符串差不多,

String.Concat会获得稍微好一点点的性能提高,

String.Format会让你使用起来更方便,

StringBuilder更适合更多更长的字符串拼接,

如果有其它见解,还请指导。

using System;
using System.Diagnostics;
using System.Text;
namespace CompareInstructionExecutionSpeed
{
    public delegate void CompareExcecutionSpeed(int loop);
    class Program
    {
        public static string ResultConcatenation = string.Empty;
        public static readonly StringBuilder Sb = new StringBuilder();
        public static readonly Stopwatch Stopwatch = new Stopwatch();

        public static void Main()
        {
            CompareExcecutionSpeed methods = StringBuilderExecutionSpeed;
            methods += StringConcatExecutionSpeed;
            methods += ManualConcatenationExecutionSpeed;
            methods += StringFormatExecutionSpeed;
            //methods+=Some Method -- you can add your method to calculate speed.

            methods.Invoke(100);//count

            Console.ReadKey();
        }

        //Elapsing StringBuilder -------------------------------------------
        public static void StringBuilderExecutionSpeed(int loop)
        {
            Stopwatch.Restart();
            for (int i = 0; i < loop; i++)
            {
                ShowPercentProgress(i, loop);
                Sb.Append(" str");
                Sb.AppendLine(i.ToString());
            }
            Stopwatch.Stop();
            ShowCompareResult("StringBuilder", Stopwatch);
        }

        //Elapsing Str1+Str2+... -------------------------------------------
        public static void ManualConcatenationExecutionSpeed(int loop)
        {
            Stopwatch.Restart();
            for (int i = 0; i < loop; i++)
            {
                ShowPercentProgress(i, loop);
                ResultConcatenation += " str" + i + "
";
            }
            Stopwatch.Stop();
            ShowCompareResult("str1+str2+...", Stopwatch);
        }

        //Elapsing String.Concat -------------------------------------------
        public static void StringConcatExecutionSpeed(int loop)
        {
            Stopwatch.Restart();
            for (int i = 0; i < loop; i++)
            {
                ShowPercentProgress(i, loop);
                ResultConcatenation += string.Concat(" str", i, "
");
            }
            Stopwatch.Stop();
            ShowCompareResult("String.Concat", Stopwatch);

        }

        //Elapsing String.Format -------------------------------------------
        public static void StringFormatExecutionSpeed(int loop)
        {
            Stopwatch.Restart();
            for (int i = 0; i < loop; i++)
            {
                ShowPercentProgress(i, loop);
                ResultConcatenation += string.Format(" str{0}
", i);
            }
            Stopwatch.Stop();
            ShowCompareResult("String.Format", Stopwatch);
        }

        //Show Compare Result---------------------------------------------
        public static void ShowCompareResult(string message, Stopwatch stopwatch)
        {
            Console.ResetColor();
            Console.WriteLine("
{0}	{1,9} Millisecond  ~={2,3} second  ~={3,3} minutes",
                message,
                Math.Round(stopwatch.Elapsed.TotalMilliseconds),
                Math.Round(stopwatch.Elapsed.TotalSeconds),
                Math.Round(stopwatch.Elapsed.TotalMinutes));
        }

        //Show processing progress----------------------------------------
        static void ShowPercentProgress(int currElementIndex, int totalElementCount)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            int percent = (100 * (currElementIndex + 1)) / totalElementCount;
            Console.Write("
{0}%", percent);
        }
    }
}
原文地址:https://www.cnblogs.com/taiyonghai/p/5702542.html