From 886884ab606def1183d99eca7dec8affefee635e Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Wed, 12 Mar 2014 08:45:44 +0100 Subject: [PATCH] Use higher resolution timestamps on Windows. The resolution of the GetTickCount function is limited to the resolution of the system timer, which is typically in the range of 10 to 16 milliseconds. This is unsufficient for measuring very short time intervals in the milliseconds range. The QueryPerformanceCounter function provides much higher resolution timestamps. We already use it for the half-duplex workaround. --- examples/utils.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/utils.c b/examples/utils.c index 46ac07f..5206bc0 100644 --- a/examples/utils.c +++ b/examples/utils.c @@ -34,7 +34,7 @@ static unsigned char g_lastchar = '\n'; #ifdef _WIN32 #include - static unsigned long g_timestamp; + static LARGE_INTEGER g_timestamp, g_frequency; #else #include static struct timeval g_timestamp; @@ -47,9 +47,12 @@ int message (const char* fmt, ...) if (g_logfile) { if (g_lastchar == '\n') { #ifdef _WIN32 - unsigned long timestamp = GetTickCount () - g_timestamp; - unsigned long sec = timestamp / 1000L, msec = timestamp % 1000L; - fprintf (g_logfile, "[%li.%03li] ", sec, msec); + LARGE_INTEGER now, timestamp; + QueryPerformanceCounter(&now); + timestamp.QuadPart = now.QuadPart - g_timestamp.QuadPart; + timestamp.QuadPart *= 1000000; + timestamp.QuadPart /= g_frequency.QuadPart; + fprintf (g_logfile, "[%I64i.%06I64i] ", timestamp.QuadPart / 1000000, timestamp.QuadPart % 1000000); #else struct timeval now, timestamp; gettimeofday (&now, NULL); @@ -89,7 +92,8 @@ void message_set_logfile (const char* filename) if (g_logfile) { g_lastchar = '\n'; #ifdef _WIN32 - g_timestamp = GetTickCount (); + QueryPerformanceFrequency(&g_frequency); + QueryPerformanceCounter(&g_timestamp); #else gettimeofday (&g_timestamp, NULL); #endif