-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.h
executable file
·158 lines (136 loc) · 3.59 KB
/
Timer.h
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef TIMER_H_
#define TIMER_H_
#ifdef _WIN32
#include <windows.h>
#include <WinNT.h>
#else
#include <sys/time.h>
#endif
/// Computes elapsed time in milliseconds returned as a double precision floating point number.
/// Works on Linux, Windows and Mac OS.
class Timer
{
public:
///Default constructor.
Timer();
///Start timer.
void Start();
///Stop timer.
///\return elapsed time in milliseconds since call to Start()
double Stop();
///Computes elapsed time in milliseconds between a Start() and Stop() calls
///\return time elapesed between subsequent Start() and Stop() function calls
double ElapsedTime() const;
///Returns the time elapsed from the call to the Start() function;
///does not stop the timer.
///\return time in milliseconds elapsed from previous call to Start()
double DTime() const;
private:
///Reset timer.
void Reset();
#ifdef WIN32
LARGE_INTEGER freq_;
LARGE_INTEGER tstart_;
LARGE_INTEGER tend_;
mutable LARGE_INTEGER tendTmp_;
#else
timeval tstart_;
timeval tend_;
mutable timeval tendTmp_;
#endif
private:
///Do not allow construction from other instance.
Timer( const Timer& );
///Forbid assignment.
Timer operator=( const Timer& );
};
///Scoped timer: starts when declared, stops and invokes callback upon destruction.
template < class CBackT >
class ScopedCBackTimer
{
public:
///Default constructor: Starts timer.
ScopedCBackTimer()
{
timer_.Start();
}
///Destrcuctor: stops timer and invokes callback.
~ScopedCBackTimer()
{ cback_( timer_.Stop() ); }
private:
CBackT cback_;
Timer timer_;
private:
///Forbid construction from other instance.
ScopedCBackTimer( const ScopedCBackTimer& );
///Forbid assignment.
ScopedCBackTimer operator=( const ScopedCBackTimer& );
};
#ifdef WIN32
inline Timer::Timer()
{
::QueryPerformanceFrequency( &freq_ );
tstart_.QuadPart = 0;
tend_.QuadPart = 0;
}
inline void Timer::Start()
{
::QueryPerformanceCounter( &tstart_ );
}
inline double Timer::Stop()
{
::QueryPerformanceCounter( &tend_ );
return DTime();
}
inline double Timer::ElapsedTime() const
{
::QueryPerformanceCounter( &tendTmp_ );
return 1000. * ( ( double ) tendTmp_.QuadPart -
( double ) tstart_.QuadPart) /( ( double ) freq_.QuadPart );
}
inline double Timer::DTime() const
{
return 1000. * ( ( double ) tend_.QuadPart -
( double ) tstart_.QuadPart) /( ( double ) freq_.QuadPart );
}
inline void Timer::Reset()
{
tstart_.QuadPart = 0;
tend_.QuadPart = 0;
}
#else
inline Timer::Timer()
{
Reset();
}
inline void Timer::Start()
{
::gettimeofday( &tstart_, 0 );
}
inline double Timer::Stop()
{
::gettimeofday( &tend_, 0 );
return DTime();
}
inline double Timer::ElapsedTime() const
{
::gettimeofday( &tendTmp_, 0 );
const double t1 = ( double ) tstart_.tv_sec + ( double ) tstart_.tv_usec / 1E6;
const double t2 = ( double ) tendTmp_.tv_sec + ( double ) tendTmp_.tv_usec / 1E6;
return 1000. * ( t2 - t1 );
}
inline double Timer::DTime() const
{
const double t1 = ( double ) tstart_.tv_sec + ( double ) tstart_.tv_usec / 1E6;
const double t2 = ( double ) tend_.tv_sec + ( double ) tend_.tv_usec / 1E6;
return 1000. * ( t2 - t1 );
}
inline void Timer::Reset()
{
tstart_.tv_sec = 0;
tstart_.tv_usec = 0;
tend_.tv_sec = 0;
tend_.tv_usec = 0;
}
#endif
#endif //TIMER_H_