出处: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.

最新文章

  1. 当前Windows群集心跳阀值设置
  2. 初识SSH框架
  3. POJ 2528 Mayor&#39;s posters (线段树)
  4. VMware克隆虚拟机,克隆机网卡启动不了解决方案
  5. Qt 信号槽如何传递参数(或带参数的信号槽)
  6. 面向侧面的程序设计AOP-------《一》概述
  7. 解决TIME_WAIT过多造成的问题
  8. .gitignore无效,不能过滤某些文件
  9. codeforces 680C C. Bear and Prime 100(数论)
  10. POJ 2349 Arctic Network (最小生成树)
  11. SDWebImage 原理及使用
  12. idea配置tomcat.md
  13. 关于swiper的滚动条滑动
  14. Confluence 6 XML 备份失败的问题解决
  15. axiso实战问题
  16. CodeForces 纪念一下这个突破性的时刻
  17. AJPFX讲解外汇保证金交易的货币符号和外汇的报价方式
  18. SpringMVC系列(九)自定义视图、重定向、转发
  19. gradle使用心得
  20. SAP ECC6安装系列四:安装过程详解

热门文章

  1. Java递归练习201908091049
  2. 《Java基础复习》—规范与基础
  3. JSP+Struts2+JDBC+Mysql实现的校园宿舍管理系统
  4. centos8系统下docker安装jenkins
  5. 万字长文带你入门Zookeeper!!!
  6. Pytest系列(9) - 参数化@pytest.mark.parametrize
  7. .NET Core项目部署到Linux(Centos7)(二)环境和软件的准备
  8. Shell:Day07.笔记
  9. 记一次mysql多表查询(left jion)优化案例
  10. python3(七)dict list