Re: Timer
- From: "William DePalo [MVP VC++]" <willd.no.spam@xxxxxxxx>
- Date: Sat, 18 Jun 2005 15:28:56 -0400
"Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@xxxxxxxxx> wrote in message
news:efIB5A9cFHA.2128@xxxxxxxxxxxxxxxxxxxxxxx
>> Sleep( 16 );
>
> Sleep(16) will sleep at least for 20 or 30 ms... (depending on the hal).
Be default that's true.
However the granularity of the timer can be adjusted with timeBeginPeriod().
The little hack below waits for two hundred .016 second periods. On my
machine - with 88 processes running the background - I'm averaging about
..0167 seconds per wait.
In fact, when I drop the wait to 1 miliisecond, over 1000 iterations the
average wait is between 1 and 2 ms.
Regards,
Will
//-----------------------------------------------------------------------------------
#include <windows.h>
#include <iostream>
#include <mmsystem.h>
using namespace std;
union MyTime
{
__int64 i64;
FILETIME ft;
};
int main()
{
int i;
MyTime u;
__int64 ibegin, iend, ielapsed;
timeBeginPeriod(1);
GetSystemTimeAsFileTime(&u.ft);
ibegin = u.i64;
for ( i = 0; i < 200; i++ )
Sleep(16);
GetSystemTimeAsFileTime(&u.ft);
iend = u.i64;
timeEndPeriod(1);
ielapsed = (iend - ibegin) / 10000;
cout << "One thousand loop iterations took " << ielapsed / 1000 << "." <<
ielapsed % 1000 << " seconds." << endl;
return 0;
}
.
- Follow-Ups:
- Re: Timer
- From: Jochen Kalmbach [MVP]
- Re: Timer