出处: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 = 0;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(1000);cout << GetCounter() <<"\n";return 0;
}

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.

转载于:https://www.cnblogs.com/liujx2019/p/10309810.html

How to use QueryPerformanceCounter? (c++,不使用 .Net)相关推荐

  1. VMware QueryPerformanceCounter/GetTickCount 悬案

    最近发现在VMware的客户机上,两次 QueryPerformanceCounter 相差的数值会特别大.还不知道如何解决.. 测试程序如下 #include <windows.h>DW ...

  2. QueryPerformanceFrequency 和 QueryPerformanceCounter用法

    QueryPerformanceFrequency() - 基本介绍 类型:Win32API 原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFr ...

  3. QueryPerformanceCounter实现Windows微秒级延时

    windows的Sleep函数,睡眠线程指定毫秒数,可以用来做毫秒延时. 对于微秒延时,没有一个现成的函数,但是可以通过 QueryPerformanceFrequency QueryPerforma ...

  4. QueryPerformanceCounter

    windows的Sleep函数,睡眠线程指定毫秒数,可以用来做毫秒延时. 对于微秒延时,没有一个现成的函数,但是可以通过 QueryPerformanceFrequency QueryPerforma ...

  5. windows:QueryPerformanceCounter函数/PerformanceCounter函数

    QueryPerformanceCounter函数:检索性能计数器的当前值,该值是可用于时间间隔测量的高分辨率(<1us)时间戳. QueryPerformanceCounter函数 Perfo ...

  6. 时间的函数Sleep,clock,GetTickCount,QueryPerformanceCounter

    介绍 我 们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执行一个特定的操作,比如在多媒体中,比如在游戏中等,都 会用到时间函数.还比如我们通过记录函数或者算 ...

  7. QueryPerformanceCounter计算时间

    QueryPerformanceCounter()这个函数返回高精确度性能计数器的值,它可以以微妙为单位计时. 计算时间的公式: double time=(nStopCounter.QuadPart- ...

  8. VC中基于 Windows 的精确定时

    方式一:VC中的WM_TIMER消息映射能进行简单的时间控制.首先调用函数SetTimer()设置定时 间隔,如SetTimer(0,200,NULL)即为设置200ms的时间间隔.然后在应用程序中增 ...

  9. memcpy()内存拷贝和赋值操作效率测试

    比较memcpy()内存拷贝和"="赋值操作效率,测试代码如下 #include <stdio.h> #include <malloc.h> #includ ...

最新文章

  1. 实战域树部署,Active Directory系列之十九
  2. Note6:batch file programming
  3. WindowsXP正版验证方法
  4. 《Delphi XE6 android 编程入门教程》推荐
  5. leetcode18. 四数之和(双指针)
  6. 快报:Python 被爆重大“黑料”!程序员:劲爆!
  7. 从零开始刷Leetcode——数组(268.283.414)
  8. java stream, LIST ,ArrayList常用操作
  9. Object类中的wait()和notify()
  10. svn将本地项目传到svn
  11. Java Date总结
  12. 当你的MS OFFICE打不开时,安全模式也失效,来看我给你变戏法吧
  13. 透视星环科技上市:基础工具、技术融合、场景应用三维击穿
  14. 【SEO实验室】使用百度SEO排名点击软件快速提升排名
  15. 【EMNLP2020】忒修斯之船启发下的知识蒸馏新思路 - 微软研究院
  16. 安卓手机突然很卡_安卓手机为什么很卡 安卓手机卡顿原因分析【详解】
  17. 快递电子运单上,电话应隐藏6位以上,禁止显示这些信息
  18. Windows Support Tools
  19. CentOS7 中 Docker-ce 安装配置 MyCat-Web 监控 MyCat状态
  20. Redhat6.5离线配置Zabbix,含自定义Zabbix监控项

热门文章

  1. 环信快速集成,以及实际集成中遇到的坑
  2. 被流氓360设置浏览器主页的解决办法(如果你也遇到了跟我一样的问题,不妨看一下是不是这个原因)...
  3. applet实现大文件ftp上传(三)
  4. [Java核心技术(卷I)] - 简易的日历
  5. vue --- 前端代理发送http请求
  6. 汇编 --- 从磁盘(扇区2到18)上读取数据到内存中
  7. 深入Spring Boot:快速集成Dubbo + Hystrix
  8. spring BeanFactory概述
  9. PostgreSQL入门篇学习笔记(七)
  10. Apache Spark源码走读之4 -- DStream实时流数据处理