#include "Timer.h" Timer::Timer() { baseTime = 0.0; PlatformInitialize(); } double Timer::GetTime() { return PlatformGetTime() - baseTime; } void Timer::Reset() { baseTime = PlatformGetTime(); } //---------------------------------------------------------------------------- // Platform-specific stuff follows: //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- #ifdef _WIN32 // Win32; use Windows QueryPerformanceFrequency API. //---------------------------------------------------------------------------- #include #include #include static bool initialized = false; static BOOL highResolution = FALSE; static LARGE_INTEGER hiResFrequency; void Timer::PlatformInitialize() { if(!initialized) { highResolution = QueryPerformanceFrequency(&hiResFrequency); initialized = true; } } double Timer::PlatformGetTime() { if(highResolution) { LARGE_INTEGER time; if(QueryPerformanceCounter(&time)) { return (double)time.QuadPart / hiResFrequency.QuadPart; } else { return NAN; } } else { return (double)clock() / CLOCKS_PER_SEC; } } //---------------------------------------------------------------------------- #else // Not Win32; use gettimeofday. //---------------------------------------------------------------------------- #include Timer::PlatformInitialize() { // Nothing to initialize for gettimeofday; } double Timer::PlatformGetTime() { timeval time; gettimeofday(&time, (void*)0); return time.tv_sec + (time.tv_usec / 1000000.0); } //---------------------------------------------------------------------------- #endif //----------------------------------------------------------------------------