11 using Clock = std::chrono::steady_clock;
27 _accumulated = Duration::zero();
28 _startTime = Clock::now();
36 _accumulated += Clock::now() - _startTime;
45 _startTime = Clock::now();
53 _accumulated = Duration::zero();
60 const Clock::time_point now = Clock::now();
61 const Duration elapsedTime = _isRunning ? _accumulated + (now - _startTime) : _accumulated;
62 _accumulated = Duration::zero();
68 [[nodiscard]]
bool isRunning() const noexcept {
return _isRunning; }
73 return _isRunning ? _accumulated + (Clock::now() - _startTime) : _accumulated;
77 template <
typename T =
double>
78 [[nodiscard]] T
getElapsedSeconds() const noexcept {
return getElapsedTime<T, std::ratio<1>>(); }
80 template <
typename T =
double>
81 [[nodiscard]] T
getElapsedMs() const noexcept {
return getElapsedTime<T, std::milli>(); }
83 template <
typename T =
double>
84 [[nodiscard]] T
getElapsedUs() const noexcept {
return getElapsedTime<T, std::micro>(); }
86 template <
typename T =
double>
87 [[nodiscard]] T
getElapsedNs() const noexcept {
return getElapsedTime<T, std::nano>(); }
90 template <
typename T,
typename Period>
91 [[nodiscard]] T getElapsedTime() const noexcept
93 return std::chrono::duration_cast<std::chrono::duration<T, Period>>(
elapsed()).count();
96 Clock::time_point _startTime{};
98 bool _isRunning =
false;
Stopwatch that can be stopped and resumed, based on a steady clock.
Definition Timer.hpp:9
void stop() noexcept
Pauses, keeping the elapsed time.
Definition Timer.hpp:33
void resume() noexcept
Continues after stop().
Definition Timer.hpp:42
Clock::duration Duration
Definition Timer.hpp:12
Duration elapsed() const noexcept
Total running time.
Definition Timer.hpp:71
T getElapsedNs() const noexcept
Definition Timer.hpp:87
T getElapsedUs() const noexcept
Definition Timer.hpp:84
static Timer startNew() noexcept
Returns a running timer.
Definition Timer.hpp:17
void reset() noexcept
Stops and clears the elapsed time.
Definition Timer.hpp:51
T getElapsedSeconds() const noexcept
Elapsed time in seconds; the Ms, Us and Ns variants use milli-, micro- and nanoseconds.
Definition Timer.hpp:78
T getElapsedMs() const noexcept
Definition Timer.hpp:81
std::chrono::steady_clock Clock
Definition Timer.hpp:11
bool isRunning() const noexcept
Definition Timer.hpp:68
void start() noexcept
Starts from zero.
Definition Timer.hpp:25
Duration restart() noexcept
Returns the elapsed time and starts again from zero.
Definition Timer.hpp:58
Stopwatch, frame timing and date formatting.
Definition DateTime.hpp:8