<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      eaglet

      本博專注于基于微軟技術的搜索相關技術
        博客園  :: 首頁  :: 新隨筆  :: 聯系 :: 訂閱 訂閱  :: 管理

      對老趙寫的簡單性能計數器的修改

      Posted on 2009-03-10 14:10  eaglet  閱讀(12875)  評論(44)    收藏  舉報

      對老趙寫的簡單性能計數器的修改

          早上看到老趙寫的這個性能計數器,感覺很實用,不過老趙用了很多.C# 3.0 的新語法,還用了 VISTA 和 Server 2008 下特有的Win32 API,對于還在用C#2.0 或者還工作在 XP 或者 Server 2003 下的兄弟們,只能望代碼心嘆了。應老趙的要求,我修改了他的代碼,增加了對低版本C# 和 低版本windows 操作系統的支持。

          老趙的原文: 一個簡單的性能計數器:CodeTimer

          修改說明

          1. 采用 接口 取代了原代碼中的 Lambda 表達式

          2. 采用 GetThreadTimes 這個API 函數替代了原代碼中的 QueryThreadCycleTime

          這里需要說明的是 GetThreadTimes 給出了線程在內核態和用戶態占用的時間,單位是 100 ns。兩個時間的總和就是線程占用的CPU時間。這個API的時間精度我看了一些資料似乎沒有達到 100ns. 所以GetThreadTimes 這個API函數的進度沒有 QueryThreadCycleTime 高。

          下面是我修改后的代碼

          注釋1: 2009-03-11 增加委托的調用,修改 GC.Collect 參數,兼容.Net 2.0.  增加每次調用時間統計 

          增加了委托調用后,我發現同樣是測試空函數,采用接口比采用委托效率要略高一些,這和我的預計基本吻合,因為委托不是單純的函數調用,具體原理超出本文范圍,我就不多說了。

       

       

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Diagnostics;
      using System.Threading;
      using System.Runtime.InteropServices;
        
       
          
      public interface IAction
          
      {
              
      void Action();
          }


          
      public static class CodeTimer
          
      {
              [DllImport(
      "kernel32.dll", SetLastError = true)]
              
      static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime,
                 
      out long lpExitTime, out long lpKernelTime, out long lpUserTime);

              [DllImport(
      "kernel32.dll")]
              
      static extern IntPtr GetCurrentThread();

              
      public delegate void ActionDelegate();

              
      private static long GetCurrentThreadTimes()
              
      {
                  
      long l;
                  
      long kernelTime, userTimer;
                  GetThreadTimes(GetCurrentThread(), 
      out l, out l, out kernelTime, 
                     
      out userTimer);
                  
      return kernelTime + userTimer;
              }


              
      static CodeTimer()
              
      {
                  Process.GetCurrentProcess().PriorityClass 
      = ProcessPriorityClass.High;
                  Thread.CurrentThread.Priority 
      = ThreadPriority.Highest;

              }


              
      public static void Time(string name, int iteration, ActionDelegate action)
              
      {
                  
      if (String.IsNullOrEmpty(name))
                  
      {
                      
      return;
                  }


                  
      if (action == null)
                  
      {
                      
      return;
                  }


                  
      //1. Print name
                  ConsoleColor currentForeColor = Console.ForegroundColor;
                  Console.ForegroundColor 
      = ConsoleColor.Yellow;
                  Console.WriteLine(name);


                  
      // 2. Record the latest GC counts
                  
      //GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
                  GC.Collect(GC.MaxGeneration);
                  
      int[] gcCounts = new int[GC.MaxGeneration + 1];
                  
      for (int i = 0; i <= GC.MaxGeneration; i++)
                  
      {
                      gcCounts[i] 
      = GC.CollectionCount(i);
                  }


                  
      // 3. Run action
                  Stopwatch watch = new Stopwatch();
                  watch.Start();
                  
      long ticksFst = GetCurrentThreadTimes(); //100 nanosecond one tick

                  
      for (int i = 0; i < iteration; i++) action();
                  
      long ticks = GetCurrentThreadTimes() - ticksFst;
                  watch.Stop();

                  
      // 4. Print CPU
                  Console.ForegroundColor = currentForeColor;
                  Console.WriteLine(
      "\tTime Elapsed:\t\t" + 
                     watch.ElapsedMilliseconds.ToString(
      "N0"+ "ms");
                  Console.WriteLine(
      "\tTime Elapsed (one time):" + 
                     (watch.ElapsedMilliseconds 
      / iteration).ToString("N0"+ "ms");

                  Console.WriteLine(
      "\tCPU time:\t\t" + (ticks * 100).ToString("N0"
                     
      + "ns");
                  Console.WriteLine(
      "\tCPU time (one time):\t" + (ticks * 100 / 
                     iteration).ToString(
      "N0"+ "ns");

                  
      // 5. Print GC
                  for (int i = 0; i <= GC.MaxGeneration; i++)
                  
      {
                      
      int count = GC.CollectionCount(i) - gcCounts[i];
                      Console.WriteLine(
      "\tGen " + i + ": \t\t\t" + count);
                  }


                  Console.WriteLine();

              }


              
      public static void Time(string name, int iteration, IAction action)
              
      {
                  
      if (String.IsNullOrEmpty(name))
                  
      {
                      
      return;
                  }


                  
      if (action == null)
                  
      {
                      
      return;
                  }


                  
      //1. Print name
                  ConsoleColor currentForeColor = Console.ForegroundColor;
                  Console.ForegroundColor 
      = ConsoleColor.Yellow;
                  Console.WriteLine(name);


                  
      // 2. Record the latest GC counts
                  
      //GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
                  GC.Collect(GC.MaxGeneration);
                  
      int[] gcCounts = new int[GC.MaxGeneration + 1];
                  
      for (int i = 0; i <= GC.MaxGeneration; i++)
                  
      {
                      gcCounts[i] 
      = GC.CollectionCount(i);
                  }


                  
      // 3. Run action
                  Stopwatch watch = new Stopwatch();
                  watch.Start();
                  
      long ticksFst = GetCurrentThreadTimes(); //100 nanosecond one tick
                  
                  
      for (int i = 0; i < iteration; i++) action.Action();
                  
      long ticks = GetCurrentThreadTimes() - ticksFst;
                  watch.Stop();

                  
      // 4. Print CPU
                  Console.ForegroundColor = currentForeColor;
                  Console.WriteLine(
      "\tTime Elapsed:\t\t" + 
                     watch.ElapsedMilliseconds.ToString(
      "N0"+ "ms");
                  Console.WriteLine(
      "\tTime Elapsed (one time):" + 
                     (watch.ElapsedMilliseconds 
      / iteration).ToString("N0"+ "ms");

                  Console.WriteLine(
      "\tCPU time:\t\t" + (ticks * 100).ToString("N0"
                      
      + "ns");
                  Console.WriteLine(
      "\tCPU time (one time):\t" + (ticks * 100 / 
                      iteration).ToString(
      "N0"+ "ns");

                  
      // 5. Print GC
                  for (int i = 0; i <= GC.MaxGeneration; i++)
                  
      {
                      
      int count = GC.CollectionCount(i) - gcCounts[i];
                      Console.WriteLine(
      "\tGen " + i + ": \t\t\t" + count);
                  }


                  Console.WriteLine();

              }

          }

         

      測試類

       

       

          public class TestSleep3000 : IAction
          {
              
      #region IAction Members

              
      public void Action()
              {
                  Thread.Sleep(
      3000);
              }

              
      #endregion
          }


          
      public class TestEmptyMethod : IAction
          {
              
      #region IAction Members

              
      public void Action()
              {
              }

              
      #endregion
          }

          
      public class TestStringConcat : IAction
          {
              
      string s = "";

              
      #region IAction Members

              
      public void Action()
              {
                  s 
      += "a";
              }

              
      #endregion
          }

          
      public class TestStringBuilderConcat : IAction
          {
              StringBuilder s 
      = new StringBuilder();

              
      #region IAction Members

              
      public void Action()
              {
                  s.Append (
      "a");
              }

              
      #endregion
          }

       

      測試代碼

      采用接口 

                  CodeTimer.Time("Thread Sleep"1new TestSleep3000());
                  CodeTimer.Time(
      "Thread Sleep"10000000new TestEmptyMethod());
                  CodeTimer.Time(
      "String Concat"100000new TestStringConcat());
                  CodeTimer.Time(
      "StringBuilder Conca"100000
                       
      new TestStringBuilderConcat()); 

       

      測試結果

       

       

      Thread Sleep
              Time Elapsed:           2,997ms
              Time Elapsed (one time):2,997ms
              CPU time:               0ns
              CPU time (one time):    0ns
              Gen 0:                  0
              Gen 1:                  0
              Gen 2:                  0

      Empty Method
              Time Elapsed:           138ms
              Time Elapsed (one time):0ms
              CPU time:               125,000,000ns
              CPU time (one time):    12ns
              Gen 0:                  0
              Gen 1:                  0
              Gen 2:                  0

      String Concat
              Time Elapsed:           10,547ms
              Time Elapsed (one time):0ms
              CPU time:               10,546,875,000ns
              CPU time (one time):    105,468ns
              Gen 0:                  4102
              Gen 1:                  2661
              Gen 2:                  2545

      StringBuilder Conca
              Time Elapsed:           4ms
              Time Elapsed (one time):0ms
              CPU time:               0ns
              CPU time (one time):    0ns
              Gen 0:                  0
              Gen 1:                  0
              Gen 2:                  0

       

      采用委托

       

      CodeTimer.Time("Thread Sleep"1delegate() { Thread.Sleep(3000); });
      CodeTimer.Time(
      "Empty Method"10000000delegate() { });

      string a = "";

      CodeTimer.Time(
      "String Concat"100000delegate() { a += "a"; });

      StringBuilder s 
      = new StringBuilder();
      CodeTimer.Time(
      "StringBuilder Conca"100000delegate() { s.Append("a"); }); 

       

       測試結果

       

      Thread Sleep
              Time Elapsed:           2,989ms
              Time Elapsed (one time):2,989ms
              CPU time:               0ns
              CPU time (one time):    0ns
              Gen 0:                  0
              Gen 1:                  0
              Gen 2:                  0

      Empty Method
              Time Elapsed:           156ms
              Time Elapsed (one time):0ms
              CPU time:               156,250,000ns
              CPU time (one time):    15ns
              Gen 0:                  0
              Gen 1:                  0
              Gen 2:                  0

      String Concat
              Time Elapsed:           10,425ms
              Time Elapsed (one time):0ms
              CPU time:               10,406,250,000ns
              CPU time (one time):    104,062ns
              Gen 0:                  4102
              Gen 1:                  2661
              Gen 2:                  2545

      StringBuilder Conca
              Time Elapsed:           4ms
              Time Elapsed (one time):0ms
              CPU time:               0ns
              CPU time (one time):    0ns
              Gen 0:                  0
              Gen 1:                  0
              Gen 2:                  0

       

       

       

       

       

       

       

      主站蜘蛛池模板: 国产综合视频一区二区三区| 亚洲精品国偷自产在线99人热| 国产一区二区三区精美视频| 在线成人国产天堂精品av| 少妇高潮潮喷到猛进猛出小说| 久久精品久久电影免费理论片| 亚洲精品美女久久7777777| 国产99久久精品一区二区| 青春草公开在线视频日韩| 国产精品自拍一二三四区| 免费无码一区无码东京热| 国产国拍精品av在线观看| 国产精品日韩中文字幕熟女| 国产精品久久国产精麻豆99网站| 久久精品国产亚洲AV成人毛片| 国产日韩入口一区二区| 乱中年女人伦av三区| 真人在线射美女视频在线观看| 婷婷四房播播| www亚洲精品| 国产亚洲av嫩草久久| 人人妻人人狠人人爽| 国产精品一级久久黄色片| 国产精品无码久久久久AV| 日日碰狠狠躁久久躁96avv| 免费观看欧美猛交视频黑人| 亚洲最大中文字幕无码网站| 欧美牲交a欧美牲交aⅴ一| 国产xxxx做受视频| 国产精品播放一区二区三区| 中文字幕日韩精品有码| 成人无码潮喷在线观看| 日本国产精品第一页久久| 亚洲AV无码国产永久播放蜜芽| 国产亚洲999精品AA片在线爽| 野外做受又硬又粗又大视频√| 爆乳喷奶水无码正在播放| 在线中文字幕国产精品| 精品无码成人片一区二区| 中文字幕精品无码一区二区三区| 激情综合色综合久久丁香|