-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathtimer.h
44 lines (38 loc) · 847 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
40
41
42
43
44
#pragma once
#include <chrono>
namespace timers {
class TimerBase {
public:
virtual void start() {}
virtual void stop() {}
float microseconds() const noexcept {
return mMs * 1000.f;
}
float milliseconds() const noexcept {
return mMs;
}
float seconds() const noexcept {
return mMs / 1000.f;
}
void reset() noexcept {
mMs = 0.f;
}
protected:
float mMs{0.0f};
};
template <typename Clock>
class CPUTimer : public TimerBase {
public:
using clock_type = Clock;
void start() {
mStart = Clock::now();
}
void stop() {
mStop = Clock::now();
mMs += std::chrono::duration<float, std::milli>{mStop - mStart}.count();
}
private:
std::chrono::time_point<Clock> mStart, mStop;
}; // class CPUTimer
using PreciseCPUTimer = CPUTimer<std::chrono::high_resolution_clock>;
} // namespace timers