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) ...
随机推荐
- cut-trailing-bytes:二进制尾部去0小工具
背景 之前的文章 二进制文件处理之尾部补0和尾部去0 中介绍了一种使用 sed 去除二进制文件尾部的 NULL(十六进制0x00)字节的方法. 最近发现这种方法有局限性,无法处理较大的文件.因为 se ...
- 关于HashCode和equals方法在HashSet中的使用
Object类是类层次结构的根类,故所有的类都是先该类的方法,其中HashCode()和equals()方法也是该类的方法. 1.HashCode()方法 Object类中HashCode()方法实现 ...
- SpringCloud入门(九): Zuul 上传&回退&异常处理&跨域
Zuul的上传 1.构建一个上传类 import org.springframework.web.bind.annotation.PostMapping; import org.springframe ...
- Pycharm 文件模板配置
Pycharm 模板配置 #!/usr/bin/python # -*- coding: UTF-8 -*- # Author:${USER} 作者 # FileName:${NAME} 文件名称 # ...
- NKOJ 1353 图形面积
时间限制 : 10000 MS 空间限制 : 65536 KB 问题描述 桌面上放了N个矩形,这N个矩形可能有互相覆盖的部分,求它们组成的图形的面积.(矩形的边都与坐标轴平行) 输入格式 输入第一 ...
- Windows上安装Docker
一.下载地址: https://hub.docker.com/editions/community/docker-ce-desktop-windows 二.安装直接下一步下一步就好了 具体可看: ht ...
- python 函数--匿名函数
一.匿名函数的定义: 解决一些简单的需要用函数去问题,匿名函数的函数体只有一行. 二.格式: calc = lambda n:n**n 函数名 = 匿名函数 参数:返回值 三.练习:
- Tomcat目录解析
bin 可执行文件的储存 conf 配置文件 lib 依赖jar包 logs 日志文件 temp 临时文件 webapps 创建的web应用程序 work 存放运行时数据 如何启动Tomcat? 启动 ...
- 配置spark历史服务(spark二)
1. 编辑spark-defaults.conf位置文件 添加spark.eventLog.enabled和spark.eventLog.dir的配置修改spark.eventLog.dir为我们之前 ...
- Markdown自动生成目录
Markdown自动生成目录 使用npm语法生成 1.安装npm 2.安装doctoc插件 3.执行生成 参考 Markdown自动生成目录 使用npm语法生成 1.安装npm 我的系统是deepin ...