Win32: use high resolution timers (QueryPerformanceCounter) if available

This commit is contained in:
Richard Harrison
2019-01-18 12:09:10 +01:00
parent 64d51b5290
commit c433d29171

View File

@@ -71,14 +71,31 @@ static clockid_t getClockId()
}
#endif
static bool qpc_init = false;
static LARGE_INTEGER s_frequency;
static BOOL s_use_qpc;
void SGTimeStamp::stamp()
{
#ifdef _WIN32
unsigned int t;
t = timeGetTime();
_sec = t / 1000;
_nsec = ( t - ( _sec * 1000 ) ) * 1000 * 1000;
if (!qpc_init) {
s_use_qpc = QueryPerformanceFrequency(&s_frequency);
qpc_init = true;
}
if (qpc_init && s_use_qpc) {
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
_sec = now.QuadPart / s_frequency.QuadPart;
_nsec = (1000000000LL * (now.QuadPart - _sec * s_frequency.QuadPart)) / s_frequency.QuadPart;
}
else {
unsigned int t;
t = timeGetTime();
_sec = t / 1000;
_nsec = (t - (_sec * 1000)) * 1000 * 1000;
}
#elif defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
struct timespec ts;
clock_gettime(getClockId(), &ts);