-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.hpp
57 lines (47 loc) · 1.13 KB
/
Timer.hpp
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
#ifndef TIMER_HPP_
#define TIMER_HPP_
#include <chrono>
#include <string>
template<typename T>
struct ttime {
enum {value = 1};
};
template<>
struct ttime<std::chrono::milliseconds> {
enum {value = 2};
};
template<>
struct ttime<std::chrono::microseconds> {
enum {value = 3};
};
template<>
struct ttime<std::chrono::nanoseconds> {
enum {value = 4};
};
inline std::string conv(int i) {
if(i == 2) {
return "milliseconds";
}
if(i == 3) {
return "microseconds";
}
if(i == 4) {
return "nanoseconds";
}
return "N/A";
}
template<typename T>
class Timer {
private:
std::chrono::high_resolution_clock::time_point t1, t2;
T elapsed;
public:
Timer() { elapsed = T(0); }
void start() { t1 = std::chrono::high_resolution_clock::now(); }
void stop() { t2 = std::chrono::high_resolution_clock::now(); elapsed += std::chrono::duration_cast<T>(t2-t1); };
T duration() { return elapsed; }
void reset() { elapsed = T(0); }
void startover() { reset(); start();}
std::string type() { return conv(ttime<T>::value);}
};
#endif