【问题标题】:High resolution timer in .NET.NET 中的高分辨率计时器
【发布时间】:2010-09-14 20:23:40
【问题描述】:

我想对我的代码进行一些基本的分析,但发现 C# 中的 DateTime.Now 的分辨率只有大约 16 毫秒。我还没有找到更好的计时结构。

【问题讨论】:

    标签: .net profiling timer


    【解决方案1】:

    下面是一个用于计时操作的示例代码:

    Dim sw As New Stopwatch()
    sw.Start()
    //Insert Code To Time
    sw.Stop()
    Dim ms As Long = sw.ElapsedMilliseconds
    Console.WriteLine("Total Seconds Elapsed: " & ms / 1000)
    

    编辑:

    巧妙的是,它也可以恢复。

    Stopwatch sw = new Stopwatch();
    foreach(MyStuff stuff in _listOfMyStuff)
    {
        sw.Start();
        stuff.DoCoolCalculation();
        sw.Stop();
    }
    Console.WriteLine("Total calculation time: {0}", sw.Elapsed);
    

    System.Diagnostics.Stopwatch 类将使用高分辨率计数器(如果您的系统上可用)。

    【讨论】:

      【解决方案2】:

      System.Diagnostics.StopWatch 类非常适合进行分析。

      如果您不想编写自己的测量函数,这里是指向Vance Morrison's Code Timer Blog 的链接。

      【讨论】:

      • 是的,那个计数高分辨率时钟(如果存在)的滴答声......正是我需要的。
      【解决方案3】:

      对于最高分辨率的性能计数器,您可以使用底层的 win32 性能计数器。

      添加以下 P/Invoke sig:

      [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
      public static extern bool QueryPerformanceCounter(out long perfcount);
      
      [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
      public static extern bool QueryPerformanceFrequency(out long freq);
      

      并使用以下方式调用它们:

      #region Query Performance Counter
      /// <summary>
      /// Gets the current 'Ticks' on the performance counter
      /// </summary>
      /// <returns>Long indicating the number of ticks on the performance counter</returns>
      public static long QueryPerformanceCounter()
      {
          long perfcount;
          QueryPerformanceCounter(out perfcount);
          return perfcount;
      }
      #endregion
      
      #region Query Performance Frequency
      /// <summary>
      /// Gets the number of performance counter ticks that occur every second
      /// </summary>
      /// <returns>The number of performance counter ticks that occur every second</returns>
      public static long QueryPerformanceFrequency()
      {
          long freq;
          QueryPerformanceFrequency(out freq);
          return freq;
      }
      #endregion
      

      将所有内容转储到一个简单的类中,您就可以开始了。示例(假设类名为 PerformanceCounters):

      long startCount = PerformanceCounter.QueryPerformanceCounter();
      // DoStuff();
      long stopCount = PerformanceCounter.QueryPerformanceCounter();
      long elapsedCount = stopCount - startCount;
      double elapsedSeconds = (double)elapsedCount / PerformanceCounter.QueryPerformanceFrequency();
      MessageBox.Show(String.Format("Took {0} Seconds", Math.Round(elapsedSeconds, 6).ToString()));
      

      【讨论】:

      • Stopwatch 类从 .NET 2.0 开始为您执行此操作。
      【解决方案4】:

      您可以调用 Windows 中的高分辨率性能计数器。函数名称为 kernel32.dll 中的 QueryPerformanceCounter。

      导入 C# 的语法:

      [DllImport("Kernel32.dll")]
      private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
      

      Windows 调用的语法:

      BOOL QueryPerformanceCounter(      
          LARGE_INTEGER *lpPerformanceCount
      );
      

      QueryPerformanceCounter @ MSDN

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-02
        • 2011-10-31
        • 1970-01-01
        相关资源
        最近更新 更多