使用QueryPerformanceCounter()计算运行时间 [英] Running time calculation using QueryPerformanceCounter()

查看:636
本文介绍了使用QueryPerformanceCounter()计算运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用QueryPerformanceCounter()计算运行时间(以微秒为单位)?请不要在此帖子中给出任何链接答案,因为我已经提到了很多链接.

How to calculate running time in microseconds using QueryPerformanceCounter()? please don''t give any links answer in this post itself because i have referred many links.

推荐答案



实际上,您必须先调用QueryPerformanceFrequency.
QueryPerformanceFrequency函数将为您提供机器中的滴答/秒.而且,如果QueryPerformanceFrequency函数返回零,则意味着您的计算机不支持高分辨率性能计数器.
请参见以下示例代码:

LARGE_INTEGER t1,t2;
LARGE_INTEGER freq;
LARGE_INTEGER ms;

if(QueryPerformanceFrequency(&freq)== 0)
{
printf(您的机器不支持高分辨率性能计数器\ n");
返回;
}

QueryPerformanceCounter(&t1);
....
QueryPerformanceCounter(&t2);

ms =(t2.QuadPart-t1.QuadPart)/(freq.QuadPart/1000);

ms在t1和t2之间会有毫秒数的差异.
Hi,

Actually you have to call QueryPerformanceFrequency first.
QueryPerformanceFrequency function will give you ticks/second in your machine. And if QueryPerformanceFrequency function return zero, means your machine not support high resolution performance counter.
Please see following example code:

LARGE_INTEGER t1, t2;
LARGE_INTEGER freq;
LARGE_INTEGER ms;

if( QueryPerformanceFrequency( &freq ) == 0 )
{
printf("Your machine not support high resolution performance counter\n");
return;
}

QueryPerformanceCounter( &t1 );
....
QueryPerformanceCounter( &t2 );

ms = (t2.QuadPart - t1.QuadPart) / (freq.QuadPart / 1000);

ms will have difference between t1 and t2 in milli-seconds.


不确定使用QueryPerformanceCounter()"是什么意思,但是您可以按以下方式计算运行时间(以微秒为单位)
Not sure what you mean by ''using QueryPerformanceCounter()'' but you can calulate runing time in microseconds as follows
DWORD dwStartTime = GetTickCount();

// do the thing you want to time here

DWORD dwEndTime = GetTickCount();

// calculate the time taken

DWORD dwTimeTaken = (dwEndTime >= dwStartTime) ? dwEndTime - dwStartTime : (0xFFFFFFFF-dwStartTime) + dwEndTime 
// (0xFFFFFFFF-dwStartTime) + dwEndTime  this is incase the tickcount wraps around


这篇关于使用QueryPerformanceCounter()计算运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆