QueryPerformanceFrequency function(profileapi.h)

Retrieves the frequency of the performance counter. The frequency of the performance counter is fixed at system boot and is consistent across all processors. Therefore, the frequency need only be queried upon application initialization, and the result can be cached.

Syntax
C++

Copy
BOOL QueryPerformanceFrequency(
[out] LARGE_INTEGER *lpFrequency
);
Parameters
[out] lpFrequency

A pointer to a variable that receives the current performance-counter frequency, in counts per second. If the installed hardware doesn’t support a high-resolution performance counter, this parameter can be zero (this will not occur on systems that run Windows XP or later).

Return value
If the installed hardware supports a high-resolution performance counter, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError. On systems that run Windows XP or later, the function will always succeed and will thus never return zero.

Remarks
For more info about this function and its usage, see Acquiring high-resolution time stamps.

Requirements
REQUIREMENTS

Minimum supported client Windows 2000 Professional [desktop apps | UWP apps]
Minimum supported server Windows 2000 Server [desktop apps | UWP apps]
Target Platform Windows
Header profileapi.h (include Windows.h)
Library Kernel32.lib
DLL Kernel32.dll

link:https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancefrequency

QueryPerformanceFrequency() - 基本介绍
类型:Win32API

原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

作用:返回硬件支持的高精度计数器的频率。

返回值:非零,硬件支持高精度计数器;零,硬件不支持,读取失败。

QueryPerformanceFrequency() - 技术特点
供WIN9X使用的高精度定时器:QueryPerformanceFrequency()和QueryPerformanceCounter(),要求计算机从硬件上支持高精度定时器。

函数的原形是:
  BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);
  BOOL QueryPerformanceCounter (LARGE_INTEGER *lpCount);

数据类型LARGEINTEGER既可以是一个作为8字节长的整数,也可以是作为两个4字节长的整数的联合结构,其具体用法根据编译器是否支持64位而定。该类型的定义如下:

  typeef union _ LARGE_INTEGER
  {
   struct
   {
   DWORD LowPart;
   LONG HighPart;
   };
   LONGLONG QuadPart;
  } LARGE_INTEGER;

在定时前应该先调用QueryPerformanceFrequency()函数获得机器内部计时器的时钟频率。接着在需要严格计时的事件发生前和发生之后分别调用QueryPerformanceCounter(),利用两次获得的计数之差和时钟频率,就可以计算出事件经历的精确时间。测试函数SLEEP(100)的精确持续时间方法:

  LARGE_INTEGER litmp;
  LONGLONG qt1,qt2;
  double dft,dff,dfm;
  QueryPerformanceFrequency(&litmp);//获得时钟频率
  dff=(double)litmp.QuadPart;
  QueryPerformanceCounter(&litmp);//获得初始值
  qt1=litmp.QuadPart;Sleep(100);
  QueryPerformanceCounter(&litmp);//获得终止值
  qt2=litmp.QuadPart;
  dfm=(double)(qt2-qt1);
  dft=dfm/dff;//获得对应的时间值

需要注意的是DFT计算的结果单位是秒。

(转载:http://hi.baidu.com/tihu1111/blog/item/1a9cf40105ae1405738da500.html)

应用:

Linux下精确至毫秒

#include <sys/time.h>
#include <iostream>
#include <time.h>
double get_wall_time()
{
    struct timeval time ;
    if (gettimeofday(&time,NULL)){
        return 0;
    }
    return (double)time.tv_sec + (double)time.tv_usec * .000001;
}
 
int main()
{
	unsigned int t = 0;
	double start_time = get_wall_time()
	while(t++<10e+6);
	double end_time = get_wall_time()
	std::cout<<"循环耗时为:"<<end_time-start_time<<"ms";
	return 0;
}

Windows下精确至毫秒

#include <windows.h>
#include <iostream>
 
int main()
{
	DWORD start, stop;
	unsigned int t = 0;
	start = GetTickCount();
	while (t++ < 10e+6);
	stop = GetTickCount();
	printf("time: %lld ms\n", stop - start);
	return 0;
}

发现貌似getTickCount函数会有10几毫秒的误差

Windows下精确至微秒

//MyTimer.h//
#ifndef __MyTimer_H__  
#define __MyTimer_H__  
#include <windows.h>  
 
class MyTimer
{
private:
	int _freq;
	LARGE_INTEGER _begin;
	LARGE_INTEGER _end;
 
public:
	long costTime;            // 花费的时间(精确到微秒)  
 
public:
	MyTimer()
	{
		LARGE_INTEGER tmp;
		QueryPerformanceFrequency(&tmp);//QueryPerformanceFrequency()作用:返回硬件支持的高精度计数器的频率。  
 
		_freq = tmp.QuadPart;
		costTime = 0;
	}
 
	void Start()            // 开始计时  
	{
		QueryPerformanceCounter(&_begin);//获得初始值  
	}
 
	void End()                // 结束计时  
	{
		QueryPerformanceCounter(&_end);//获得终止值  
		costTime = (long)((_end.QuadPart - _begin.QuadPart) * 1000000 / _freq);
	}
 
	void Reset()            // 计时清0  
	{
		costTime = 0;
	}
};
#endif  
 
//main.cpp
#include "MyTimer.h"
#include <iostream>
 
 
int main()
{
	MyTimer timer;
	unsigned int t = 0; 
	timer.Start();
	while (t++ < 10e+5);
	timer.End(); 
	std::cout << "耗时为:" << timer.costTime << "us";
	return 0 ;
}

参考:https://blog.csdn.net/AP1005834/article/details/53419647?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163789279916780366555115%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163789279916780366555115&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-1-53419647.first_rank_v2_pc_rank_v29&utm_term=get_wall_time&spm=1018.2226.3001.4187

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值