-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiming-chrono.h
More file actions
48 lines (40 loc) · 1.3 KB
/
timing-chrono.h
File metadata and controls
48 lines (40 loc) · 1.3 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
#ifndef GUT_TIMINGCHRONO_H
#define GUT_TIMINGCHRONO_H
#include <chrono>
namespace gut {
class Duration {
typedef std::chrono::duration<double> duration_t;
duration_t seconds_;
public:
explicit Duration(double seconds) : seconds_(seconds) {}
template<class Rep, class Period>
explicit Duration(std::chrono::duration<Rep, Period> duration)
: seconds_(std::chrono::duration_cast<duration_t>(duration)) {}
double seconds() const { return seconds_.count(); }
template<class T>
bool operator>(const T& duration) const { return seconds_ > duration; }
bool operator>(int duration) const { return seconds_.count() > duration; }
bool operator>(double duration) const {
return seconds_.count() > duration;
}
};
class Timer {
std::chrono::steady_clock::time_point start_;
public:
Timer() { reset (); }
void reset() { start_ = std::chrono::steady_clock::now(); }
Duration elapsedTime() {
return Duration(std::chrono::steady_clock::now() - start_);
}
};
std::string toString(const Duration& value) {
std::ostringstream oss;
oss << value.seconds() << "s";
return oss.str();
}
template<class T, class U>
Duration match_duration(const T& source, const U& /*target*/) {
return Duration(source);
}
} // namespace gut
#endif // GUT_TIMINGCHRONO_H