-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.h
114 lines (99 loc) · 2.88 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
#ifndef TIMER_H
#define TIMER_H
#include <chrono>
#include <string>
#include <iostream>
/* \return The current time in seconds */
int time_s() {
using namespace std::chrono;
return static_cast<int>( duration_cast<seconds>(system_clock::now().time_since_epoch()).count() );
};
/* \return The current time in ms */
int time_ms() {
using namespace std::chrono;
return static_cast<int>( duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count() );
};
/* \return The current time in us */
int time_us() {
using namespace std::chrono;
return static_cast<int>( duration_cast<microseconds>(system_clock::now().time_since_epoch()).count() );
};
/* \return The current time in ns */
int time_ns() {
using namespace std::chrono;
return static_cast<int>( duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count() );
};
/* Different units of time */
enum TIME_UNIT {
s = 0,
ms,
us,
ns
};
/* A class to handle simple timing. */
class Timer {
public:
/* \param[in] unit Unit of time to track. */
Timer(TIME_UNIT unit=TIME_UNIT::ms) {
unit_ = unit;
restart();
};
/* Restart the timer. */
void restart() {
timer_ = get_time_();
};
/* Get the current value of the timer. */
int get() {
return get_time_() - timer_;
}
/* Print the value of the timer.
* \param[in] message an optional message to show before the timer value */
void print(std::string message="") {
int clock = get_time_() - timer_;
std::cout << message;
if (message != "") std::cout << " ";
std::cout << clock << " ";
switch(unit_) {
case s:
std::cout << "s" << std::endl;
break;
case ms:
std::cout << "ms" << std::endl;
break;
case us:
std::cout << "us" << std::endl;
break;
case ns:
std::cout << "ns" << std::endl;
break;
default:
std::cout << std::endl;
break;
}
}
private:
int timer_;
TIME_UNIT unit_;
int get_time_() {
int time = 0;
switch(unit_) {
case s:
time = time_s();
break;
case ms:
time = time_ms();
break;
case us:
time = time_us();
break;
case ns:
time = time_ns();
break;
default:
time = time_ms();
break;
}
return time;
};
};
#endif