diff --git a/src/openalpr/support/timing.cpp b/src/openalpr/support/timing.cpp index 0b66891..5b259f8 100644 --- a/src/openalpr/support/timing.cpp +++ b/src/openalpr/support/timing.cpp @@ -1,35 +1,103 @@ #include "timing.h" -#ifdef __MACH__ -#include -#include -#endif #ifdef WINDOWS timespec diff(timespec start, timespec end); + +// Windows timing code +LARGE_INTEGER getFILETIMEoffset() +{ + SYSTEMTIME s; + FILETIME f; + LARGE_INTEGER t; + + s.wYear = 1970; + s.wMonth = 1; + s.wDay = 1; + s.wHour = 0; + s.wMinute = 0; + s.wSecond = 0; + s.wMilliseconds = 0; + SystemTimeToFileTime(&s, &f); + t.QuadPart = f.dwHighDateTime; + t.QuadPart <<= 32; + t.QuadPart |= f.dwLowDateTime; + return (t); +} + +int clock_gettime(int X, timespec *tv) +{ + LARGE_INTEGER t; + FILETIME f; + double microseconds; + static LARGE_INTEGER offset; + static double frequencyToMicroseconds; + static int initialized = 0; + static BOOL usePerformanceCounter = 0; + + if (!initialized) { + LARGE_INTEGER performanceFrequency; + initialized = 1; + usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency); + if (usePerformanceCounter) { + QueryPerformanceCounter(&offset); + frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.; + } else { + offset = getFILETIMEoffset(); + frequencyToMicroseconds = 10.; + } + } + if (usePerformanceCounter) QueryPerformanceCounter(&t); + else { + GetSystemTimeAsFileTime(&f); + t.QuadPart = f.dwHighDateTime; + t.QuadPart <<= 32; + t.QuadPart |= f.dwLowDateTime; + } + + t.QuadPart -= offset.QuadPart; + microseconds = (double)t.QuadPart / frequencyToMicroseconds; + t.QuadPart = microseconds; + tv->tv_sec = t.QuadPart / 1000000; + tv->tv_usec = t.QuadPart % 1000000; + return (0); +} + + + void getTime(timespec* time) { - // Do nothing on Windows + clock_gettime(0, time); + } double diffclock(timespec time1,timespec time2) { - // Mock this out for Windows - return 0; + timespec delta = diff(time1,time2); + double milliseconds = (delta.tv_sec * 1000) + (((double) delta.tv_usec) / 1000.0); + + + return milliseconds; } timespec diff(timespec start, timespec end) { - // Mock this out for Windows - return 0; + timespec temp; + if ((end.tv_usec-start.tv_usec)<0) { + temp.tv_sec = end.tv_sec-start.tv_sec-1; + temp.tv_usec = 1000000+end.tv_usec-start.tv_usec; + } else { + temp.tv_sec = end.tv_sec-start.tv_sec; + temp.tv_usec = end.tv_usec-start.tv_usec; + } + return temp; } #else -timespec diff(timespec start, timespec end); void getTime(timespec* time) { diff --git a/src/openalpr/support/timing.h b/src/openalpr/support/timing.h index e7cabb3..66763f7 100644 --- a/src/openalpr/support/timing.h +++ b/src/openalpr/support/timing.h @@ -3,10 +3,17 @@ #include +// Support for OS X +#ifdef __MACH__ +#include +#include +#endif +// Support for Windows #ifdef WINDOWS - // Mock this out for Windows - #define timespec int + #include + + #define timespec timeval #endif void getTime(timespec* time);