您的位置:首页 > 其它

MFC 中怎么实现微秒级的延时

2012-10-12 11:24 471 查看
转自百度文库:

DelayUs

void DelayUs(int uDelay)
{

LARGE_INTEGER litmp;
LONGLONG QPart1,QPart2;

double dfMinus,dfFreq,dfTim;

/*
Pointer to a variable that the function sets, in counts per second, to the current performance-counter frequency.
If the installed hardware does not support a high-resolution performance counter,
the value passed back through this pointer can be zero.

*/
QueryPerformanceFrequency(&litmp);

dfFreq = (double)litmp.QuadPart;

/*
Pointer to a variable that the function sets, in counts, to the current performance-counter value.
*/
QueryPerformanceCounter(&litmp);

QPart1 = litmp.QuadPart;
do
{
QueryPerformanceCounter(&litmp);
QPart2 = litmp.QuadPart;
dfMinus = (double)(QPart2-QPart1);
dfTim = dfMinus/dfFreq;
}while(dfTim<0.000001 * uDelay);
}


如果你看懂了上述函数,还可以用它里面的部分代码来测试很多函数的执行时间。

当然,这个函数还是有点隐患,如果在一个延时期间,定时器溢出,那么这个值就有可能不准,需要你小心处理。

如:if (dfMinus < 0)

    break;

这个假设硬件定时器是以增加的方式计数的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: