-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.h
39 lines (35 loc) · 894 Bytes
/
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
/*
* Simple C++ timer
*
* Author: [email protected]
*/
#include <chrono>
class Timer {
public:
enum class TimeUnit {
kSecs,
kMsecs,
kUsecs
};
void Start() {
t_ = std::chrono::high_resolution_clock::now();
}
// Stop the timer and return the time since start in 'unit' time units.
int64_t Stop(const TimeUnit unit) {
auto delta = std::chrono::high_resolution_clock::now() - t_;
switch (unit) {
case TimeUnit::kSecs:
return
std::chrono::duration_cast<std::chrono::seconds>(delta).count();
case TimeUnit::kMsecs:
return
std::chrono::duration_cast<std::chrono::milliseconds>(delta).count();
case TimeUnit::kUsecs:
return
std::chrono::duration_cast<std::chrono::microseconds>(delta).count();
}
throw -1;
}
private:
std::chrono::high_resolution_clock::time_point t_;
};