如果一个系统包含高精度性能计数器(HRPC,high-resolution performance counter)则此系统提供高精度定时器。你可以使用API函数QueryPerformanceFrequency来获得HRPC的频率HRPCF,返回值为cps(counts per second)。这个依赖于处理器(processor dependent),在一些处理器中HRPCF的值可能就是处理器时钟周期(the cycle rate of the processor clock),比如我们测试,在3.0GHZ的Pentium4处理器(Windows2000 Professional)上得到HSPCF=,在一个2.8GHZ的Pentium4处理器(Windows XP)上得到HSPCF=,而在一个2.4GHZ的Pentium4处理器(Windows2000 Server)上得到HSPCF= 。

使用另外一个函数QueryPerformanceCounter来获取(retrieve)HRPC的值,QPF和QPC的函数原型分别为:

BOOL QueryPerformanceFrequency(
LARGE_INTEGER *lpFrequency
);
BOOL QueryPerformanceCounter(
LARGE_INTEGER *lpPerformanceCount
);
QPC和QPF函数都在winbase.h中定义。函数中用到的数据类型可以参考如下 typedef __int64 LONGLONG;
typedef unsigned __int64 ULONGLONG
typedef unsigned long DWORD; typedef union union {
struct {
DWORD LowPart;
LONG HighPart;
};
LONGLONG QuadPart;
} LARGE_INTEGER, *PLARGE_INTEGER;
在一段代码的前后调用两次HPC函数,就能知道这段代码执行消耗了多少CPU周期或时间,这对于不同算法的性能测试很有帮助,下面有一个例子来进行演示。另外利用循环也可以实现定时功能。 那么怎么根据得到的两个HRPC计算时间呢?因为知道了HRPCF是表示HRPC的频率,所以就可以知道HRPC数值每增加1所代表的时间: HRPC second per count dHMSecondPerCount = 1.0 / HRPCF ; //spc HRPC millisecond per count dHMMillisecondPerCount = 1.0E3 / HRPCF ; //mspc HRPC microsecond per count dHMMicrosecondPerCount = 1.0E6 / HRPCF ; //uspc 知道这三个值就可以根据两个HRPC差值(difference),即diff=HRPC2-HRPC1,这样就可以计算出两次调用QPC函数经过(elapse)的时间: QueryPerformanceCounter( &HRPC1); //code to be timed QueryPerformanceCounter( &HRPC2); diff=HRPC2-HRPC1; Elapsed second = diff * dHMSecondPerCount ; Elapsed millisecond = diff * dHMMillisecondPerCount ; Elapsed microsecond = diff * dHMMicrosecondPerCount ; 例程如下: /****************************
author:Wandy
time: 2005-11-25
file: demoForHighResolutionTimer.cpp
version:1.0
environment:VS.NET2003 + Windows2000Server
abstract:demo for High-Resolution Timer
*****************************/
#i nclude <windows.h>
#i nclude <stdio.h> void main() { LARGE_INTEGER liFrequency;
LARGE_INTEGER liStart;
LARGE_INTEGER liEnd;
LONGLONG liDiff;//or LARGE_INTEGER diff; double dHMSecondPerCount;//how many second per count
double dHMMillisecondPerCount;//how many millisecond per count
double dHMMicrosecondPerCount;//how many microsecond per count //set the current Process and Thresd to a high priority
DWORD dwOldProcessP = GetPriorityClass(GetCurrentProcess());
DWORD dwOldThreadP = GetThreadPriority(GetCurrentThread());
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); // Save the performance counter frequency for later use.
if ( !QueryPerformanceFrequency( &liFrequency ) )
printf( "QPF() failed with error %d\n", GetLastError() );
else
printf( "HRPCF: %I64d \n",liFrequency.QuadPart ); //
dHMSecondPerCount = 1.0 / (double)liFrequency.QuadPart;
dHMMillisecondPerCount = 1.0E3 / (double)liFrequency.QuadPart;
dHMMicrosecondPerCount = 1.0E6 / (double)liFrequency.QuadPart;
printf("dHMSecondPerCount: %g spc \n", dHMSecondPerCount);
printf("dHMMillisecondPerCount:%g mspc\n", dHMMillisecondPerCount);
printf("dHMMicrosecondPerCount:%g uspc\n", dHMMicrosecondPerCount); //start
if ( !QueryPerformanceCounter( &liStart ) )
printf( "QPC() failed with error %d\n", GetLastError() );
else
printf( "start HRPC: %I64d \n", liStart.QuadPart ); //code to be timed
Sleep( ); //end
if ( !QueryPerformanceCounter( &liEnd ) )
printf( "QPC() failed with error %d \n", GetLastError() );
else
printf( "end HRPC: %I64d \n", liEnd.QuadPart ); //print diff
liDiff = liEnd.QuadPart - liStart.QuadPart; printf( "HRPC diff:%I64d and ms elapsed: %fms\n", liDiff,(double)liDiff * dHMMillisecondPerCount ); //restore the process and thread priority
SetThreadPriority(GetCurrentThread(), dwOldThreadP);
SetPriorityClass(GetCurrentProcess(), dwOldProcessP); //wait for press any key
getchar();
}
在我的机器上的执行结果为
HRPCF:
dHMSecondPerCount: 2.79365e-007 spc
dHMMillisecondPerCount:0.000279365 mspc
dHMMicrosecondPerCount:0.279365 uspc
start HRPC:
end HRPC:
HRPC diff: and ms elapsed: .153511ms

关于High-Resolution Timer(了解)的更多相关文章

  1. Timer计时不准确的解决方案 每次都重新调整,修正误差

    http://stackoverflow.com/questions/29722838/system-timers-timer-steadily-increasing-the-interval 需要在 ...

  2. 初探linux子系统集之timer子系统(一)

    一般来说要让整个linux系统跑起来,那么一个必须的就是linux的时钟,也就是时间子系统了,这里正好工作需要,那么就研究下linux下的时间子系统了. linux内核必须完成两种主要的定时测量.一个 ...

  3. [WorldWind学习]18.High-Performance Timer in C#

    In some applications exact time measurement methods are very important. 一些应用程序中精确的时间测量是非常重要的. The of ...

  4. windows和linux平台下的通用时间测试函数

    Time.cpp ////////////////////////////////////////////////////////////////////////////// // Timer.cpp ...

  5. grub paramiter & menu.list

    在Linux中,给kernel传递参数以控制其行为总共有三种方法: 1.build kernel之时的各个configuration选项. 2.当kernel启动之时,可以参数在kernel被GRUB ...

  6. [转]设置Android手机以使用ARM Streamline进行性能分析(二)

    原文因为arm社区改版访问不到了,原作者鲍方,原文地址,这篇是从google cache里挖出来的,希望能帮到要对cocos2dx优化的各位   Posted by Fang Bao, Leave C ...

  7. Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现

    转自:http://blog.csdn.net/droidphone/article/details/8074892 上一篇文章,我介绍了传统的低分辨率定时器的实现原理.而随着内核的不断演进,大牛们已 ...

  8. 戴文的Linux内核专题:06配置内核(2)

    转自Linux中国 这一部分我们讲配置内核IRQ子系统.中断请求(IRQ)是硬件发给处理器的一个信号,它暂时停止一个正在运行的程序并允许一个特殊的程序占用CPU运行. 这个目录中的第一个问题属于内核特 ...

  9. net programming guid

    Beej's Guide to Network Programming Using Internet Sockets Brian "Beej Jorgensen" Hallbeej ...

  10. 【Android 系统开发】 编译 Android文件系统 u-boot 内核 并烧写到 OK-6410A 开发板上

    博客地址 : http://blog.csdn.net/shulianghan/article/details/40299813  本篇文章中用到的工具源码下载 : -- ok-6410A 附带的 A ...

随机推荐

  1. 【原创】解决鼠标经过子元素触发mouseout,mouseover事件的问题

    关键词:父子元素关系  mouseout  mouseover  事件  事件冒泡 初期代码: <!DOCTYPE html> <html> <head> < ...

  2. js JSON对象属性

    json对象是是一种用于原生json分析的对象,无法对其进行构造函数调用,用java术语 来说,它相当于能够直接使用类方法的工具类JSON对象的属性parse(text[,reviver]);对参数t ...

  3. Asp.net Form登陆认证的回顾学习

    asp.net网站中,我最常用的就是Form认证了,在实现登陆时,利用Form认证实现用户的访问权限,哪些页面是可以匿名登陆,哪些页面需要认证后才能访问,哪些页面不能访问等等权限.我还可在登陆时,使用 ...

  4. C++文件操作(输入输出、格式控制、文件打开模式、测试流状态、二进制读写)

    1.向文件写数据 头文件#include <ofstream> ①Create an instance of ofstream(创建ofstream实例) ②Open the file w ...

  5. 蓝牙4.0LED灯控方案

    一.LED照明机遇 相对传统光源产品,LED灯凭借其光效高.寿命长.不含汞.总拥有成本低等优势,已被普遍认为是一种革命性和替代性的技术.随着全球白炽灯禁产.禁用政策的依次落实,白炽灯将逐渐消失于市场. ...

  6. IOS下载资源zip到本地然后读取

    思路是 1.ios下载服务器上的zip资源包(图片,声音等经过zip压缩的资源包)到本地 2.解压zip到程序目录 3.从程序目录加载资源文件 一.下载zip资源 [cpp]-(NSString*)D ...

  7. python之ftplib库

    检测ftp是否可用 #!/usr/bin/python #coding:utf-8 from ftplib import FTP def ftp_open(ip,user,passwd): try: ...

  8. 老工程升级到VS2010或以上时会出现 libc.lib 解决方法

    有些网上的工程都比较老,比如用2003之类.一般会有个静态libc.lib.在新版本里已经没有这个库,被微软无情的抛弃. 编译时会出现动态库找不到: 1>LINK : fatal error L ...

  9. 【转】Winform下KeyDown,KeyPress,KeyUp事件的总结

    http://blog.csdn.net/xiashengwang/article/details/6777907

  10. MVC4.0网站发布和部署到IIS7.0上的方法【转:http://www.th7.cn/Program/net/201403/183756.shtml】

    最近在研究MVC4,使用vs2010,开发的站点在发布和部署到iis7上的过程中遇到了很多问题,现在将解决的过程记录下来,以便日后参考,整个过程主要以截图形式呈现 vs2010的安装和mvc4的安装不 ...