-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImAppStopWatch.h
35 lines (30 loc) · 1.05 KB
/
ImAppStopWatch.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
#pragma once
#include <algorithm>
#include <chrono>
namespace ImApp {
enum class StopWatchType { Normal, Max };
template <StopWatchType TimerType> class StopWatchTimer {
public:
StopWatchTimer() {}
inline void start() {
_beginTime = std::chrono::high_resolution_clock::now();
}
inline void stop() {
_endTime = std::chrono::high_resolution_clock::now();
if constexpr (_timerType == StopWatchType::Normal) {
_us = std::chrono::duration_cast<std::chrono::microseconds>(_endTime -
_beginTime)
.count();
} else if constexpr (_timerType == StopWatchType::Max) {
_us = std::max(_us, std::chrono::duration_cast<std::chrono::microseconds>(
_endTime - _beginTime)
.count());
}
}
inline long microseconds() const { return _us; }
private:
std::chrono::system_clock::time_point _beginTime{}, _endTime{};
long _us{};
static constexpr StopWatchType _timerType{TimerType};
};
} // namespace ImApp