关于High-Resolution Timer(了解)
如果一个系统包含高精度性能计数器(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(了解)的更多相关文章
- Timer计时不准确的解决方案 每次都重新调整,修正误差
http://stackoverflow.com/questions/29722838/system-timers-timer-steadily-increasing-the-interval 需要在 ...
- 初探linux子系统集之timer子系统(一)
一般来说要让整个linux系统跑起来,那么一个必须的就是linux的时钟,也就是时间子系统了,这里正好工作需要,那么就研究下linux下的时间子系统了. linux内核必须完成两种主要的定时测量.一个 ...
- [WorldWind学习]18.High-Performance Timer in C#
In some applications exact time measurement methods are very important. 一些应用程序中精确的时间测量是非常重要的. The of ...
- windows和linux平台下的通用时间测试函数
Time.cpp ////////////////////////////////////////////////////////////////////////////// // Timer.cpp ...
- grub paramiter & menu.list
在Linux中,给kernel传递参数以控制其行为总共有三种方法: 1.build kernel之时的各个configuration选项. 2.当kernel启动之时,可以参数在kernel被GRUB ...
- [转]设置Android手机以使用ARM Streamline进行性能分析(二)
原文因为arm社区改版访问不到了,原作者鲍方,原文地址,这篇是从google cache里挖出来的,希望能帮到要对cocos2dx优化的各位 Posted by Fang Bao, Leave C ...
- Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现
转自:http://blog.csdn.net/droidphone/article/details/8074892 上一篇文章,我介绍了传统的低分辨率定时器的实现原理.而随着内核的不断演进,大牛们已 ...
- 戴文的Linux内核专题:06配置内核(2)
转自Linux中国 这一部分我们讲配置内核IRQ子系统.中断请求(IRQ)是硬件发给处理器的一个信号,它暂时停止一个正在运行的程序并允许一个特殊的程序占用CPU运行. 这个目录中的第一个问题属于内核特 ...
- net programming guid
Beej's Guide to Network Programming Using Internet Sockets Brian "Beej Jorgensen" Hallbeej ...
- 【Android 系统开发】 编译 Android文件系统 u-boot 内核 并烧写到 OK-6410A 开发板上
博客地址 : http://blog.csdn.net/shulianghan/article/details/40299813 本篇文章中用到的工具源码下载 : -- ok-6410A 附带的 A ...
随机推荐
- 一个伪ajax图片上传代码的例子
一个伪ajax图片上传实现代码. 复制代码代码如下: <?php if($_FILES){ ?> <script> window.parent.ajaxUploadPi ...
- Spring中Quartz调度器的使用
一.Quartz的特点 * 按作业类的继承方式来分,主要有以下两种: 1.作业类继承org.springframework.scheduling.quartz.QuartzJobBean类的方式 2. ...
- Java实现九九乘法表的输出
九九乘法表一般为三角形,每个数分别和从1到自身的数相乘然后把结果列出来,即要用到两层循环,外层是从1到9for(i=1;i<=9;i++),内层是当前数和从1到自身相乘for(j=1;j< ...
- MongoDB探索之路(一)——入门
1.MongoDB和传统关系型数据库的比较 2.面向文档的 NoSQL 数据库主要解决的问题不是高性能的并发读写,而是保证海量数据存储的同时,具有良好的查询性能. 3.MongoDB可以作为日志分 ...
- linux设备驱动层次
USB 采用树形拓扑结构,主机侧和设备侧的USB 控制器分别称为主机控制器(HostController)和USB 设备控制器(UDC),每条总线上只有一个主机控制器,负责协调主机和设备间的通信,而设 ...
- Python初学者笔记(3):输出列表中的奇数/奇数项,字符串中的偶数项,字符串大小写转换
[1]a=[8,13,11,6,26,19,24]1)请输出列表a中的奇数项2)请输出列表a中的奇数 解:1) a=[8,13,11,6,26,19,24] print a[::2] Result:& ...
- JavaScript 组件化开发之路(一)
*:first-child{margin-top: 0 !important}.markdown-body>*:last-child{margin-bottom: 0 !important}.m ...
- SSIS包配置动态配置数据库连接
动态连接数据库便于维护 用SSIS包配置实现 1.控制流页签 - 右键 - 包配置 2.配置xml文件 3.指定连接属性:ServerName.UserName.Password 测试: 1.配置错误 ...
- DB2表结构DDL脚本导出
db2look是导出DDL语句脚本的命令,以下是对db2look的一个简单介绍. 语法:db2look -d <数据库名> -e -t <表名> -o <文件名>. ...
- Redis 配置文件 redis.conf 项目详解
Redis.conf 配置文件详解 # [Redis](http://yijiebuyi.com/category/redis.html) 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, ...