• 高精度计时QueryPerformanceCounter/QueryPerformanceFrequency


    using      System;   
         using      System.Runtime.InteropServices;   
         using      System.Threading;   
        
         public      class      Test   
         {   
             [DllImport("kernel32.dll")]   
             private      static      extern      bool      QueryPerformanceFrequency(ref      Int64      lpFrequency);   
            
             [DllImport("kernel32.dll")]   
             private      static      extern      bool      QueryPerformanceCounter(ref      Int64      lpPerformanceCount);   
        
             public      static      void      Main(string      []      args)   
             {   
                 long      frequency      =      0;   
                 if(!QueryPerformanceFrequency(ref      frequency))   
                 {   
                     Console.WriteLine("not      supported.");   
                     return;   
                 }   
                 long      start      =      0;   
                 if(!QueryPerformanceCounter(ref      start))   
                 {   
                     Console.WriteLine("query      start      failed.");   
                     return;   
                 }   
                 Thread.Sleep(1000);   
                 long      end      =      0;   
                 if(!QueryPerformanceCounter(ref      end))   
                 {   
                     Console.WriteLine("query      end      failed.");   
                     return;   
                 }   
                 Console.WriteLine("frequency      =      {0},      total      time      {1}",   
                     frequency,      (double)(end      -      start)/((double)frequency));   
                
             }   
         }   
        
         结果应当和下面差不多(frequency会有差异)   
         frequency      =      1193182,      total      time      0.999754438132657   

    在 Visual C# .NET 中使用 QueryPerformanceCounter 测定代码的运行用时

    概要

    在对代码进行测试以找出性能瓶颈时,您希望使用系统所能提供的精度最高的计时器。 本分步指南描述如何使用 QueryPerformanceCounter 函数测定应用程序代码的运行用时。

    备注: JScript .NET 不能调用 Microsoft Windows API 函数。

    生成和运行演示应用程序

    1. 启动 Visual Studio .NET 并新建一个 Visual C# 控制台应用程序。

    2.

    将默认代码替换为以下代码,其操作计时的增量为 100:

    using System;
                using System.Runtime.InteropServices;
                namespace csConPerfCounter
                {
                class Class1
                {
                   [DllImport("kernel32.dll")]
                   extern static short QueryPerformanceCounter(ref long x);
                   [DllImport("kernel32.dll")]
                   extern static short QueryPerformanceFrequency(ref long x);
                   static void Main(string[] args)
                   {
                    long ctr1 = 0, ctr2 = 0, freq = 0;
                    int acc = 0, i = 0;
                    if (QueryPerformanceCounter(ref ctr1)!=0) // Begin timing.
                    {
                     for (i=0; i<100; i++) acc++;   // Code being timed.
                     QueryPerformanceCounter(ref ctr2); // Finish timing.
                     Console.WriteLine("Start Value: " + ctr1);
                     Console.WriteLine("End Value: " + ctr2);
                     QueryPerformanceFrequency(ref freq);
                     Console.WriteLine("QueryPerformanceCounter minimum resolution: 1/" + freq + " seconds.");
                     Console.WriteLine("100 Increment time: " + (ctr2 - ctr1) * 1.0 / freq + " seconds.");
                    }
                    else
                        Console.WriteLine("High-resolution counter not supported.");
                    // Make the console window wait.
                    Console.WriteLine();
                    Console.Write("Press Enter to finish ... ");
                    Console.Read();
                   }
                }
                }
    3. 保存应用程序,按 F5 键编译并运行该应用程序。控制台窗口的输出显示应类似于如下所示:
    Start Value: 281060816204
                End Value: 281060816269
                QueryPerformanceCounter minimum resolution: 1/3579545 seconds.
                100 Increment time: 1.81587324646009E-05 seconds.
                Press Enter to finish ...
    4. 按 ENTER 键停止运行应用程序并关闭控制台窗口。
  • 相关阅读:
    mybatis文件映射之select操作返回List集合
    mybatis文件映射之使用#取值时的一些规则
    mybatis文件映射之获取参数值时#和$的区别
    Java之对象池
    Java多线程设计模式(6)两阶段终止模式
    Future和FutureTask
    Java多线程设计模式(3)读写锁模式
    Java中的对象池模式
    创建资源池(对象池)
    Java多线程设计模式之线程池模式
  • 原文地址:https://www.cnblogs.com/wmz/p/1262672.html
Copyright © 2020-2023  润新知