这是c++斑竹aogun的回答
在定时之前,先调用QueryPerformanceFrequency()函数获得时钟频率, 然后在事件发生前后分别调用QueryPerformanceCounter()函数,通过计数之差及时钟频率,计算出事件经 历的精确时间
下面的例子是测试int i = 0及上下语句执行间的时间间隔,时间单位为秒,以double类型为精度
例子:
程序代码:
#include <iostream>
#include <Windows.h>
using namespace std;
void main(void)
{
LONGLONG begin_time;
LONGLONG end_time;
double time_fre;
double time_elapsed;
LARGE_INTEGER litmp;
QueryPerformanceFrequency(&litmp);
time_fre = (double)litmp.QuadPart;
QueryPerformanceCounter((LARGE_INTEGER *)&begin_time);
int i = 0; //测试int i = 0及上下语句执行间的时间间隔,秒为单位
QueryPerformanceCounter((LARGE_INTEGER *)&end_time);
time_elapsed = (end_time - begin_time) / time_fre;
cout.precision(20);
cout<<"time elapsed:"<<time_elapsed<<endl;
}