C get system time to microsecond accuracy on windows? [duplicate]
Possible Duplicate:
measuring time with re开发者_高级运维solution of microseconds in c++?
Hi,
Is there a simple way i can get the system time on a Windows machine, down to microsecond accuracy?
Look at GetSystemTimeAsFileTime
It gives you accuracy in 0.1 microseconds or 100 nanoseconds.
Note that it's Epoch different from POSIX Epoch.
So to get POSIX time in microseconds you need:
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
unsigned long long tt = ft.dwHighDateTime;
tt <<=32;
tt |= ft.dwLowDateTime;
tt /=10;
tt -= 11644473600000000ULL;
So in such case time(0) == tt / 1000000
Like this
unsigned __int64 freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
double timerFrequency = (1.0/freq);
unsigned __int64 startTime;
QueryPerformanceCounter((LARGE_INTEGER *)&startTime);
//do something...
unsigned __int64 endTime;
QueryPerformanceCounter((LARGE_INTEGER *)&endTime);
double timeDifferenceInMilliseconds = ((endTime-startTime) * timerFrequency);
What we really need is a high-resolution GetTickCount()
. As far as I know, this doesn't really exist.
If you're willing to use a hackish way to solve this (that would probably only work on some versions of Windows like XP), look here at ReactOS. Then try this code:
long long GetTickCount64()
{
return (long long)
((((unsigned long long)*(unsigned long int*)0x7FFE0000
* (unsigned long long)*(unsigned long int*)0x7FFE0004)
* (unsigned long long)10000) >> 0x18);
}
Tweaking it might give you what you need in some versions of Windows.
精彩评论