zoukankan      html  css  js  c++  java
  • 字符串连接性能

       class Program
       {
          private static readonly string STR = "0123456789";
          private static string longSTR = StringList(1024, STR);
    
          static void Main(string[] args)
          {
             Test(10000, STR);
             Test(10, longSTR);
          }
    
          public static void Test(int count, string str)
          {
             for (int i = 2; i <= 1024; i *= 2)
             {
                Time(string.Format("Nomal  Concat ({0})", i), count, () => Concat(i, str));
                Time(string.Format("StringBulider ({0})", i), count, () => StringBulider(i, str));
                Time(string.Format("String   List ({0})", i), count, () => StringList(i, str));
                Console.WriteLine();
             }
          }
    
          public static void Time(string name, int count, Func<string> function)
          {
             var watch = new Stopwatch();
             watch.Start();
             for (int i = 0; i < count; i++)
             {
                function();
             }
             watch.Stop();
             Console.WriteLine(name + watch.ElapsedTicks);
             watch.Reset();
          }
    
          public static string Concat(int count, string str)
          {
             var result = string.Empty;
             for (int i = 0; i < count; i++)
             {
                result += str + "|";
             }
             return result;
          }
    
          public static string StringBulider(int count, string str)
          {
             var result = new StringBuilder();
             for (int i = 0; i<count; i ++)
             {
                result.Append(str + "|");
             }
             return result.ToString();
          }
    
          public static string StringList(int count, string str)
          {
             var result = new List<string>();
             for (int i = 0; i < count; i++)
             {
                result.Add(str);
             }
             return string.Join("|", result);
          }
       }
    

      

  • 相关阅读:
    Linux开机启动详解
    git配置多用户多平台
    CentOS7 启动docker.service失败(code=exited, status=1/FAILURE)
    Linux 利用lsof命令恢复删除的文件
    56.storm 之 hello world (集群模式)
    55.storm 之 hello word(本地模式)
    54.Storm环境搭建
    53.storm简介
    深入浅出Mybatis-分页
    storm:最火的流式处理框架
  • 原文地址:https://www.cnblogs.com/xiao-hei/p/4920929.html
Copyright © 2011-2022 走看看