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 + "\n";
            }
            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, "\n");
            }
            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}\n", i);
            }
            Stopwatch.Stop();
            ShowCompareResult("String.Format", Stopwatch);
        }

        //Show Compare Result---------------------------------------------
        public static void ShowCompareResult(string message, Stopwatch stopwatch)
        {
            Console.ResetColor();
            Console.WriteLine("\r{0}\t{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("\r{0}%", percent);
        }
    }
}

 

转载于:https://www.cnblogs.com/taiyonghai/p/5702542.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值