summaryrefslogtreecommitdiffstats
path: root/lib/stopwatch.h
blob: a6ee2a2d796032076d702b9cdd5d6cb662073ce6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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