-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.h
124 lines (104 loc) · 2.51 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
#ifndef TIMER_H
#define TIMER_H
#ifdef _MSC_VER
// Define this if you want to enable the high resolution timer on Windows
#define USE_WIN_MSEC_TIMER
#ifdef USE_WIN_MSEC_TIMER
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <time.h>
#endif
/// Simple timer.
///
/// @author Mario Valle - Swiss National Supercomputing Centre (CSCS)
/// @date 2010-08-31 (initial version)
/// @version 1.1
///
class Timer {
public:
/// Constructor
///
Timer() : mDelta(0) {
#ifdef USE_WIN_MSEC_TIMER
QueryPerformanceFrequency(&mFreq);
#endif
start();
}
/// Start the timer
///
void start(void) {
#ifndef USE_WIN_MSEC_TIMER
time(&mStartTime);
#else
QueryPerformanceCounter(&mStartTime);
#endif
}
/// Stop the timer
///
/// @return The elapsed time in milliseconds
///
time_t stop(void) {
#ifndef USE_WIN_MSEC_TIMER
mDelta = (time(NULL) - mStartTime) * 1000L;
#else
LARGE_INTEGER end_time_w;
QueryPerformanceCounter(&end_time_w);
mDelta = static_cast<time_t>(
((end_time_w.QuadPart - mStartTime.QuadPart) * 1000) / mFreq.QuadPart);
#endif
return mDelta;
}
/// Return the elapsed time (after a start/stop cycle)
///
/// @return The elapsed time in milliseconds
///
time_t get(void) const { return mDelta; }
private:
#ifndef USE_WIN_MSEC_TIMER
time_t mStartTime; ///< The start time
#else
LARGE_INTEGER mFreq; ///< The timer frequency
LARGE_INTEGER mStartTime; ///< The start time
#endif
time_t mDelta; ///< The elapsed time in milliseconds
};
#else
#include <sys/time.h> // gettimeofday
/// Simple timer
///
/// @author Mario Valle - Swiss National Supercomputing Centre (CSCS)
/// @date 2010-08-31 (initial version)
/// @version 1.1
///
class Timer {
public:
/// Constructor
///
Timer() : mDelta(0) { start(); }
/// Start the timer
///
void start(void) { gettimeofday(&mStartTime, NULL); }
/// Stop the timer
///
/// @return The elapsed time in milliseconds
///
time_t stop(void) {
struct timeval end_time;
gettimeofday(&end_time, NULL);
mDelta = end_time.tv_sec * 1000000L + end_time.tv_usec;
mDelta -= mStartTime.tv_sec * 1000000L + mStartTime.tv_usec;
mDelta /= 1000L;
return mDelta;
}
/// Return the elapsed time (after a start/stop cycle)
///
/// @return The elapsed time in milliseconds
///
time_t get(void) const { return mDelta; }
private:
struct timeval mStartTime; ///< The start time
time_t mDelta; ///< The elapsed time in milliseconds
};
#endif
#endif