-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.hpp
More file actions
154 lines (115 loc) · 3.22 KB
/
Timer.hpp
File metadata and controls
154 lines (115 loc) · 3.22 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#pragma once
#include "../core/Types.hpp"
namespace Caffeine::Core {
struct TimePoint {
u64 ticks = 0;
TimePoint() = default;
explicit TimePoint(u64 t) : ticks(t) {}
bool operator<(const TimePoint& other) const {
return ticks < other.ticks;
}
bool operator<=(const TimePoint& other) const {
return ticks <= other.ticks;
}
bool operator>(const TimePoint& other) const {
return ticks > other.ticks;
}
bool operator>=(const TimePoint& other) const {
return ticks >= other.ticks;
}
bool operator==(const TimePoint& other) const {
return ticks == other.ticks;
}
bool operator!=(const TimePoint& other) const {
return ticks != other.ticks;
}
};
struct Duration {
f64 seconds = 0.0;
Duration() = default;
explicit Duration(f64 s) : seconds(s) {}
static Duration fromSeconds(f64 s) {
return Duration(s);
}
static Duration fromMillis(f64 ms) {
return Duration(ms / 1000.0);
}
static Duration fromMicros(f64 us) {
return Duration(us / 1000000.0);
}
static Duration fromNanos(f64 ns) {
return Duration(ns / 1000000000.0);
}
f64 millis() const {
return seconds * 1000.0;
}
f64 micros() const {
return seconds * 1000000.0;
}
f64 nanos() const {
return seconds * 1000000000.0;
}
Duration operator+(const Duration& other) const {
return Duration(seconds + other.seconds);
}
Duration operator-(const Duration& other) const {
return Duration(seconds - other.seconds);
}
Duration operator*(f64 scalar) const {
return Duration(seconds * scalar);
}
friend Duration operator*(f64 scalar, const Duration& d) {
return Duration(scalar * d.seconds);
}
Duration operator/(f64 scalar) const {
return Duration(seconds / scalar);
}
bool operator<(const Duration& other) const {
return seconds < other.seconds;
}
bool operator<=(const Duration& other) const {
return seconds <= other.seconds;
}
bool operator>(const Duration& other) const {
return seconds > other.seconds;
}
bool operator>=(const Duration& other) const {
return seconds >= other.seconds;
}
bool operator==(const Duration& other) const {
const f64 epsilon = 1e-15;
return (seconds - other.seconds) < epsilon && (seconds - other.seconds) > -epsilon;
}
bool operator!=(const Duration& other) const {
return !(*this == other);
}
};
inline Duration operator-(const TimePoint& a, const TimePoint& b) {
u64 tick_diff = (a.ticks > b.ticks) ? (a.ticks - b.ticks) : (b.ticks - a.ticks);
return Duration::fromSeconds(static_cast<f64>(tick_diff) / 1000000.0);
}
class Timer {
public:
Timer();
~Timer();
void start();
void stop();
void reset();
Duration elapsed() const;
Duration tick();
bool isRunning() const;
private:
TimePoint m_start_time;
TimePoint m_stop_time;
u64 m_accumulated_ticks;
bool m_is_running;
};
class ScopeTimer {
public:
explicit ScopeTimer(const char* name);
~ScopeTimer();
private:
const char* m_name;
Timer m_timer;
};
}