forked from DawyD/bm3d-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopwatch.hpp
More file actions
125 lines (105 loc) · 2.17 KB
/
stopwatch.hpp
File metadata and controls
125 lines (105 loc) · 2.17 KB
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
#ifndef _STOPWATCH_HPP_
#define _STOPWATCH_HPP_
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <time.h>
#include <sys/times.h>
#include <sys/time.h>
#endif
/**
* \brief Implementation of high precision wall-time stopwatch based on system timers.
*/
class Stopwatch
{
private:
typedef unsigned long long ticks_t;
ticks_t mStartTime;
double mInterval;
bool mTiming;
/**
* \brief Get current system timer status in ticks.
*/
ticks_t now()
{
#ifdef WIN32
LARGE_INTEGER ticks;
::QueryPerformanceCounter(&ticks);
return static_cast<ticks_t>(ticks.QuadPart);
#else
struct timespec ts;
::clock_gettime(CLOCK_REALTIME, &ts);
return static_cast<ticks_t>(ts.tv_sec) * 1000000000UL + static_cast<ticks_t>(ts.tv_nsec);
#endif
}
/**
* Measure current time and update mInterval.
*/
void measureTime()
{
#ifdef WIN32
LARGE_INTEGER ticks;
::QueryPerformanceFrequency(&ticks);
mInterval += static_cast<double>(now() - mStartTime) / static_cast<double>(ticks.QuadPart);
#else
mInterval += static_cast<double>((now() - mStartTime)*1E-9);
#endif
}
public:
/**
* \brief Create new stopwatch. The stopwatch are not running when created.
*/
Stopwatch() : mTiming(false), mInterval(0.0) { }
/**
* \brief Create new stopwatch (and optionaly start it).
* \param start If start is true, the stapwatch are started immediately.
*/
Stopwatch(bool start)
{
if (start) this->start();
}
/**
* \brief Start the stopwatch. If the stopwatch are already timing, they are reset.
*/
void start()
{
mTiming = true;
mStartTime = now();
}
/**
* \brief Stop the stopwatch. Multiple invocation has no effect.
*/
void stop()
{
if (mTiming == false) return;
mTiming = false;
measureTime();
}
/**
* \brief Stop and reset the stopwatch. Multiple invocation has no effect.
*/
void reset()
{
mInterval = 0.0;
if (mTiming == false) return;
mTiming = false;
}
/**
* \brief Return measured time in seconds.
*/
double getSeconds()
{
if (mTiming)
measureTime();
return mInterval;
}
/**
* \brief Return mesured time in miliseconds.
*/
double getMiliseconds()
{
return getSeconds() * 1000.0;
}
};
#endif