![]() |
#2
Lyone2011-03-29 09:14
|

QueryPerformanceFrequency(pFrquency);//获得CPU的值。
pSleepCount->QuadPart = pFrquency->QuadPart*SleepMSTime;//SleepMSTime是每次循环希望间隔的时间,单位是毫秒
LARGE_INTEGER StartCount,DoingCount;
LONG Times = 0;
QueryPerformanceCounter(&StartCount);
for (;;)
{
Doing();//自定义的一段程序
Times++;
do
{
QueryPerformanceCounter(&DoingCount);
} while ((DoingCount.QuadPart-StartCount.QuadPart)*1000 < pSleepCount->QuadPart*Times);
}
用实例测下来。SleepMSTime越大,误差也越大。我自己分析了一下。SleepMSTime的变化,最大的影响应该是do...while这段吧?可是我已经用到了Times这个标志数,来判断当前时间DoingCount与最初StartCount的差了呀?为什么还会有误差呢?pSleepCount->QuadPart = pFrquency->QuadPart*SleepMSTime;//SleepMSTime是每次循环希望间隔的时间,单位是毫秒
LARGE_INTEGER StartCount,DoingCount;
LONG Times = 0;
QueryPerformanceCounter(&StartCount);
for (;;)
{
Doing();//自定义的一段程序
Times++;
do
{
QueryPerformanceCounter(&DoingCount);
} while ((DoingCount.QuadPart-StartCount.QuadPart)*1000 < pSleepCount->QuadPart*Times);
}
[ 本帖最后由 Lyone 于 2011-3-28 15:10 编辑 ]