diff options
author | Kyle K <kylek389@gmail.com> | 2010-10-03 18:30:04 -0500 |
---|---|---|
committer | Kamil Kaminski <kamilkss@gmail.com> | 2010-10-03 18:30:04 -0500 |
commit | 163b963b1d6c1d0d288e27aeb978ff521a241917 (patch) | |
tree | d4b03049e501f8f068e4cba3a910c1fb04232ad5 /lib/stopwatch.h | |
download | GLPyramid++-163b963b1d6c1d0d288e27aeb978ff521a241917.tar.gz GLPyramid++-163b963b1d6c1d0d288e27aeb978ff521a241917.tar.bz2 GLPyramid++-163b963b1d6c1d0d288e27aeb978ff521a241917.zip |
Initial commit
Diffstat (limited to 'lib/stopwatch.h')
-rw-r--r-- | lib/stopwatch.h | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/stopwatch.h b/lib/stopwatch.h new file mode 100644 index 0000000..a6ee2a2 --- /dev/null +++ b/lib/stopwatch.h @@ -0,0 +1,77 @@ +// StopWatch.h +// Stopwatch class for high resolution timing. +// Code by Richard S. Wright Jr. +// March 23, 1999 +// +// This function uses the High performance counter on Win32 and +// gettimeofday on Mac OS X/Linux. gettimeofday is actually pretty +// good on the Mac (about 10ms). + +#ifndef STOPWATCH_HEADER +#define STOPWATCH_HEADER + +#ifdef WIN32 +#include <windows.h> +#else +#include <sys/time.h> +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Simple Stopwatch class. Use this for high resolution timing +// purposes (or, even low resolution timings) +// Pretty self-explanitory.... +// Reset(), or GetElapsedSeconds(). +class CStopWatch +{ + public: + CStopWatch(void) + { + #ifdef WIN32 + QueryPerformanceFrequency(&m_CounterFrequency); + QueryPerformanceCounter(&m_LastCount); + #else + gettimeofday(&m_LastCount, 0); + #endif + } + + // Resets timer (difference) to zero + inline void Reset(void) + { + #ifdef WIN32 + QueryPerformanceCounter(&m_LastCount); + #else + gettimeofday(&m_LastCount, 0); + #endif + } + + // Get elapsed time in seconds + float GetElapsedSeconds(void) + { + // Get the current count + #ifdef WIN32 + LARGE_INTEGER lCurrent; + QueryPerformanceCounter(&lCurrent); + + return float((lCurrent.QuadPart - m_LastCount.QuadPart) / + double(m_CounterFrequency.QuadPart)); + #else + timeval lcurrent; + gettimeofday(&lcurrent, 0); + float fSeconds = (float)(lcurrent.tv_sec - m_LastCount.tv_sec); + float fFraction = (float)(lcurrent.tv_usec - m_LastCount.tv_usec) * 0.000001f; + return fSeconds + fFraction; + #endif + } + + protected: + #ifdef WIN32 + LARGE_INTEGER m_CounterFrequency; + LARGE_INTEGER m_LastCount; + #else + timeval m_LastCount; + #endif +}; + + +#endif + |