出处: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)的更多相关文章

  1. windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime

    http://gmd20.blog.163.com/blog/static/168439232012113111759514/ 执行 10000000 次, 耗时 2258,369 微秒     Qu ...

  2. 时间的函数,sleep,clock,gettickcount,QueryPerformanceCounter(转)

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

  3. QueryPerformanceFrequency 和 QueryPerformanceCounter用法

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

  4. 获取高精度时间注意事项 (QueryPerformanceCounter , QueryPerformanceFrequency)

    花了很长时间才得到的经验,与大家分享. 1. RDTSC - 粒度: 纳秒级 不推荐优势: 几乎是能够获得最细粒度的计数器抛弃理由: A) 定义模糊- 曾经据说是处理器的cycle counter,但 ...

  5. (转)windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime

    执行 10000000 次, 耗时 2258,369 微秒     QueryPerformanceCounter 执行 10000000 次, 耗时 26,347 微秒    GetTickCoun ...

  6. C# 精准计时之 QueryPerformanceCounter QueryPerformanceFrequency用法

    C# 用法: public static class QueryPerformanceMethd { [DllImport("kernel32.dll")] public exte ...

  7. Windows 各种计时函数总结(QueryPerformanceCounter可以达到微秒)

    本文对Windows平台下常用的计时函数进行总结,包括精度为秒.毫秒.微秒三种精度的5种方法.分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及clock ...

  8. 【VS开发】QueryPerformanceFrequency与QueryPerformanceCounter的使用

    LARGE_INTEGER tima,timb; QueryPerformanceCounter(&tima); 在 Windows Server 2003 和 WindowsXP 中使用 Q ...

  9. Delphi QueryPerformanceCounter、QueryPerformanceFrequency函数,精确定时到ns

    var t1,t2:int64; r1,r2,r3:double; begin QueryPerformanceFrequency(c1);//WINDOWS API 返回计数频率 (Intel86: ...

  10. 精度试验结果报告Sleep, GetTickCount, timeGetTime, QueryPerformanceCounter

    一段简单的代码来实现精度试验 int main() {       // 初始化代码       ......       int i = 0;       while(i++ < 1000) ...

随机推荐

  1. 实验十一 MySQLl备份与恢复1

    实验十一 MySQL备份与恢复 一.  实验内容: 1. 使用SQL语句导入和导出表数据 2. 使用客户端工具备份还原数据库 3. 使用日志文件恢复数据库 二.  实验项目:学生成绩数据库 创建用于学 ...

  2. Vulnhub DC-7靶机渗透

    信息搜集 nmap -sP 192.168.146.0/24 #主机发现 nmap -A 192.168.146.144 #端口扫描 查看robots.txt,看看admin,403,其他没有什么可利 ...

  3. Vulnhub DC-3靶机渗透

    修改错误配置 打开了ova文件会发现,怎么也找不到DC-3的ip地址,估计是网卡出了问题. 那么就先配置下网卡. 进入上面这个页面之前按e. 将这里的ro 替换为 rw signie init=/bi ...

  4. Tkinter 控件

    文章更新于:2020-02-19 待翻译跟进 In this part of the Tkinter tutorial, we will cover some basic Tkinter widget ...

  5. 加锁的位置 (eq:map<key,map<>> 双集合 怎么 只加锁 在用到的对象位置,而不是把整个集合锁住)

    比如上边的map里套map 定义变量为data,例如组队副本 npc 为1 下有众多房间 即Map<1,<roomId,room>> ,处于多线程下,一个线程在 npc为1的下 ...

  6. Linux学习6-安装Python3.6

    前言 今天我们来学习一下如何在Linux环境下安装Python3.6吧! 一:下载Python3.6(我下载的是python3.6.8) 地址:https://www.python.org/downl ...

  7. 传智博客2015年最新版iOS基础视频_最适合初学者入门

    视频介绍: 本视频是iOS学院精心录制的免费精华版iOS语言基础视频,该视频特点在于最大程度保证了知识点的完整性,按知识点进行视频录制,每个视频控制在20分钟左右,不会使学生产生疲劳,授课讲究通俗易懂 ...

  8. 复杂Excel转换与导入

    需求 把不同客户提供Excel 直接导入到系统中生成对应的收货单或是出货单.后端创建收货端和出货单的接口已经有现成的webservice或是标准的xml:这类需要做的就是把客户提供不同种类的Excel ...

  9. 【spring 国际化】springMVC、springboot国际化处理详解

    在web开发中我们常常会遇到国际化语言处理问题,那么如何来做到国际化呢? 你能get的知识点? 使用springgmvc与thymeleaf进行国际化处理. 使用springgmvc与jsp进行国际化 ...

  10. JAVA—HashMap

    一些关于hashmap的学习笔记 1.HashMap底层实现原理 在JDK1.7中HashMap是以数组加链表的形式组成的,在JDK1.8之后新增了红黑树的组成结构,当链表大于8并且容量大于64时,链 ...