RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
Timer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <ratio>
5
6namespace RST::Time {
7
9 class Timer {
10 public:
11 using Clock = std::chrono::steady_clock;
12 using Duration = Clock::duration;
13
14 Timer() noexcept = default;
15
17 [[nodiscard]] static Timer startNew() noexcept
18 {
19 Timer timer;
20 timer.start();
21 return timer;
22 }
23
25 void start() noexcept
26 {
27 _accumulated = Duration::zero();
28 _startTime = Clock::now();
29 _isRunning = true;
30 }
31
33 void stop() noexcept
34 {
35 if (_isRunning) {
36 _accumulated += Clock::now() - _startTime;
37 _isRunning = false;
38 }
39 }
40
42 void resume() noexcept
43 {
44 if (!_isRunning) {
45 _startTime = Clock::now();
46 _isRunning = true;
47 }
48 }
49
51 void reset() noexcept
52 {
53 _accumulated = Duration::zero();
54 _isRunning = false;
55 }
56
58 Duration restart() noexcept
59 {
60 const Clock::time_point now = Clock::now();
61 const Duration elapsedTime = _isRunning ? _accumulated + (now - _startTime) : _accumulated;
62 _accumulated = Duration::zero();
63 _startTime = now;
64 _isRunning = true;
65 return elapsedTime;
66 }
67
68 [[nodiscard]] bool isRunning() const noexcept { return _isRunning; }
69
71 [[nodiscard]] Duration elapsed() const noexcept
72 {
73 return _isRunning ? _accumulated + (Clock::now() - _startTime) : _accumulated;
74 }
75
77 template <typename T = double>
78 [[nodiscard]] T getElapsedSeconds() const noexcept { return getElapsedTime<T, std::ratio<1>>(); }
79
80 template <typename T = double>
81 [[nodiscard]] T getElapsedMs() const noexcept { return getElapsedTime<T, std::milli>(); }
82
83 template <typename T = double>
84 [[nodiscard]] T getElapsedUs() const noexcept { return getElapsedTime<T, std::micro>(); }
85
86 template <typename T = double>
87 [[nodiscard]] T getElapsedNs() const noexcept { return getElapsedTime<T, std::nano>(); }
88
89 private:
90 template <typename T, typename Period>
91 [[nodiscard]] T getElapsedTime() const noexcept
92 {
93 return std::chrono::duration_cast<std::chrono::duration<T, Period>>(elapsed()).count();
94 }
95
96 Clock::time_point _startTime{};
97 Duration _accumulated{};
98 bool _isRunning = false;
99 };
100}
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
Timer() noexcept=default
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