How to use QueryPerformanceCounter? (c++,不使用 .Net)
出处:https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter
参考:https://docs.microsoft.com/zh-cn/windows/desktop/WmiSdk/accessing-performance-data-in-c--#example
#include <windows.h> double PCFreq = 0.0;
__int64 CounterStart = ; void StartCounter()
{
LARGE_INTEGER li;
if(!QueryPerformanceFrequency(&li))
cout << "QueryPerformanceFrequency failed!\n"; PCFreq = double(li.QuadPart)/1000.0; QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
}
double GetCounter()
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return double(li.QuadPart-CounterStart)/PCFreq;
} int main()
{
StartCounter();
Sleep();
cout << GetCounter() <<"\n";
return ;
}
This program should output a number close to 1000 (windows sleep isn't that accurate, but it should be like 999).
The StartCounter() function records the number of ticks the performance counter has in the CounterStart variable. The GetCounter() function returns the number of milliseconds since StartCounter() was last called as a double, so if GetCounter() returns 0.001 then it has been about 1 microsecond since StartCounter() was called.
If you want to have the timer use seconds instead then change
PCFreq = double(li.QuadPart)/1000.0;
to
PCFreq = double(li.QuadPart);
or if you want microseconds then use
PCFreq = double(li.QuadPart)/1000000.0;
But really it's about convenience since it returns a double.
How to use QueryPerformanceCounter? (c++,不使用 .Net)的更多相关文章
- windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime
http://gmd20.blog.163.com/blog/static/168439232012113111759514/ 执行 10000000 次, 耗时 2258,369 微秒 Qu ...
- 时间的函数,sleep,clock,gettickcount,QueryPerformanceCounter(转)
介绍 我 们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执行一个特定的操作,比如在多媒体中,比如在游戏中等,都 会用到时间函数.还比如我们通过记录函数或者算 ...
- QueryPerformanceFrequency 和 QueryPerformanceCounter用法
QueryPerformanceFrequency() - 基本介绍 类型:Win32API 原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFr ...
- 获取高精度时间注意事项 (QueryPerformanceCounter , QueryPerformanceFrequency)
花了很长时间才得到的经验,与大家分享. 1. RDTSC - 粒度: 纳秒级 不推荐优势: 几乎是能够获得最细粒度的计数器抛弃理由: A) 定义模糊- 曾经据说是处理器的cycle counter,但 ...
- (转)windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime
执行 10000000 次, 耗时 2258,369 微秒 QueryPerformanceCounter 执行 10000000 次, 耗时 26,347 微秒 GetTickCoun ...
- C# 精准计时之 QueryPerformanceCounter QueryPerformanceFrequency用法
C# 用法: public static class QueryPerformanceMethd { [DllImport("kernel32.dll")] public exte ...
- Windows 各种计时函数总结(QueryPerformanceCounter可以达到微秒)
本文对Windows平台下常用的计时函数进行总结,包括精度为秒.毫秒.微秒三种精度的5种方法.分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及clock ...
- 【VS开发】QueryPerformanceFrequency与QueryPerformanceCounter的使用
LARGE_INTEGER tima,timb; QueryPerformanceCounter(&tima); 在 Windows Server 2003 和 WindowsXP 中使用 Q ...
- Delphi QueryPerformanceCounter、QueryPerformanceFrequency函数,精确定时到ns
var t1,t2:int64; r1,r2,r3:double; begin QueryPerformanceFrequency(c1);//WINDOWS API 返回计数频率 (Intel86: ...
- 精度试验结果报告Sleep, GetTickCount, timeGetTime, QueryPerformanceCounter
一段简单的代码来实现精度试验 int main() { // 初始化代码 ...... int i = 0; while(i++ < 1000) ...
随机推荐
- M - 诡异的楼梯 HDU - 1180(BFS + 在某个点等待一下 / 重复走该点)
M - 诡异的楼梯 HDU - 1180 Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯 ...
- 1-1. OSS/ALSA 声卡的驱动与配置和 Madplay 嵌入式播放器的移植
报警子系统 一. OSS/ALSA 声卡的驱动与配置 声卡驱动中传统的OSS构架在02年被收购后即不开源,并且OSS的混音效果不好->因此ALSA构架孕育而生. ALSA(高级音频构架,目前应用 ...
- Socket探索1-两种Socket服务端实现
介绍 一次简单的Socket探索之旅,分别对Socket服务端的两种方式进行了测试和解析. CommonSocket 代码实现 实现一个简单的Socket服务,基本功能就是接收消息然后加上结束消息时间 ...
- MTK Android SwitchPreference(设置-智能辅助-导航栏-导航栏可隐藏)
1.界面布局文件 packages/apps/PrizeSettings/res/xml/navigation_bar_prize.xml ------------------------------ ...
- python 递归、匿名函数、
1.递归:就是函数自己调用自己.(注:递归最多循环999) 2.匿名函数(意义:减少内存占用) lambada 定义一个匿名函数,eg:lambad x,b:x+b (:前面是入参eg:x,b,:后 ...
- 中阶d01-- web前端 html css js bootstrap
html 页面骨架结构css 页面优化js(脚本语言) 页面和用户互动 bootstrap 前端框架,主要实现不同设备直接打开页面时播放比例设置(全屏暂时,不要滚动条)
- 01-启动jmeter目录功能
1.bin :存储了jmeter的可执行程序,如启动脚本.配置程序 docs: api扩展文档存放 lib: lib\ext 存储了jmeter的整合的功能(如.jar文件程序,和第三方 ...
- Silverlight 2.5D RPG游戏技巧与特效处理:(十一)AI系统
Silverlight 2.5D RPG游戏技巧与特效处理:(十一)AI系统 作者: 深蓝色右手 来源: 博客园 发布时间: 2011-04-19 11:18 阅读: 1282 次 推荐: 0 ...
- Property [*****] not found on type [com.erp.pojo.ErpSupplier]
我实体类里用的是 springboot 里@Slf4j @Data 注解式写的 这样可以减少代码量 ,但是遇到一个问题影响我好长时间 出现了这个错误 Property [*****] not ...
- python教程:使用 async 和 await 协程进行并发编程
python 一直在进行并发编程的优化, 比较熟知的是使用 thread 模块多线程和 multiprocessing 多进程,后来慢慢引入基于 yield 关键字的协程. 而近几个版本,python ...