zoukankan      html  css  js  c++  java
  • C# 简单测试string和StringBuilder

    测试代码如下:

      class Program
      {
        static void Main(string[] args)
        {
          string strText = "Hello from all the guys at Wrox Press. ";
          strText += "We do hope you enjoy this book as much as we enjoyed writing it.";
          Stopwatch time1 = new Stopwatch();
          time1.Start();
          for (int j = 0; j < 1000000; j++)
            for (int i = 'z'; i <= 'a'; i--)
            {
              char oldChar = (char)i;
              char newChar = (char)(i + 1);
              strText.Replace(oldChar, newChar);
            }
          for (int j = 0; j < 1000000; j++)
            for (int i = 'Z'; i <= 'A'; i--)
            {
              char oldChar = (char)i;
              char newChar = (char)(i + 1);
              strText.Replace(oldChar, newChar);
            }
          time1.Stop();
          double totalSeconds1 = time1.Elapsed.TotalMilliseconds;
    
          StringBuilder sbText = new StringBuilder("Hello from all the guys at Wrox Press. ");
          sbText.Append("We do hope you enjoy this book as much as we enjoyed writing it.");
    
          Stopwatch time2 = new Stopwatch();
          time2.Start();
          for (int j = 0; j < 1000000; j++)
            for (int i = 'z'; i <= 'a'; i--)
            {
              char oldChar = (char)i;
              char newChar = (char)(i + 1);
              sbText.Replace(oldChar, newChar);
            }
          for (int j = 0; j < 1000000; j++)
            for (int i = 'Z'; i <= 'A'; i--)
            {
              char oldChar = (char)i;
              char newChar = (char)(i + 1);
              sbText.Replace(oldChar, newChar);
            }
          time2.Stop();
          double totalSeconds2 = time2.Elapsed.TotalMilliseconds;
    
          Console.WriteLine("string用时是:       {0:F20}毫秒", totalSeconds1);
          Console.WriteLine("StringBuilder用时是:{0:F20}毫秒", totalSeconds2);
          Console.ReadKey();
        }
      }

    结果:

  • 相关阅读:
    poj 1698 二分图多重匹配
    poj 3207 2-sat
    hdu4932 Miaomiao's Geometry
    hdu4924 Football Manager
    hdu4914 Linear recursive sequence
    hdoj4906 Our happy ending(2014 Multi-University Training Contest 4)
    poj1987 Distance Statistics
    poj3342 Party at Hali-Bula
    C/C++ 调用qsort/sort 对字符数组排序的cmp函数写法
    poj1947 Rebuilding Roads
  • 原文地址:https://www.cnblogs.com/wouldguan/p/2462348.html
Copyright © 2011-2022 走看看