delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
Timer.h
1
8#pragma once
9
10#include <chrono>
11#include <cstdint>
12#include <ratio>
13#include <type_traits>
14
15namespace delpi {
16
17// Forward declaration. Found later in this file
18struct user_clock;
19
25template <typename T>
26class TimerBase {
27 public:
28 using clock = T;
29 typedef typename clock::duration duration;
30 typedef typename clock::time_point time_point;
31
33 TimerBase();
34
40 void Start();
41
47 void Pause();
48
54 void Resume();
55
57 [[nodiscard]] bool is_running() const;
59 [[nodiscard]] duration elapsed() const;
61 [[nodiscard]] std::chrono::duration<double>::rep seconds() const;
62
63 TimerBase<T> &operator+=(const TimerBase<T> &other);
64 TimerBase<T> operator+(const TimerBase<T> &other) const;
65
66 protected:
68 [[nodiscard]] time_point now() const { return clock::now(); }
69
70 private:
71 bool running_{false};
72 time_point last_start_{};
73 duration elapsed_{};
74};
75
76template <typename T>
77TimerBase<T> &TimerBase<T>::operator+=(const TimerBase<T> &other) {
78 elapsed_ += other.elapsed();
79 return *this;
80}
81
82template <typename T>
83TimerBase<T> TimerBase<T>::operator+(const TimerBase<T> &other) const {
84 TimerBase<T> result = *this;
85 result += other;
86 return result;
87}
88
89// Use high_resolution clock if it's steady, otherwise use steady_clock.
90using chosen_steady_clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady,
91 std::chrono::high_resolution_clock, std::chrono::steady_clock>;
92
93extern template class TimerBase<chosen_steady_clock>;
95class Timer : public TimerBase<chosen_steady_clock> {};
96
102 typedef uint64_t rep;
103 typedef std::micro period;
104 typedef std::chrono::duration<rep, period> duration;
105 typedef std::chrono::time_point<user_clock> time_point;
106 const bool is_steady = false; // Not sure how this should be interpreted here
107 static time_point now();
108};
109
110extern template class TimerBase<user_clock>;
112class UserTimer : public TimerBase<user_clock> {};
113
133 public:
144 TimerGuard(Timer *timer, bool enabled, bool start_timer = true);
145
146 TimerGuard(const TimerGuard &) = delete;
147 TimerGuard(TimerGuard &&) = delete;
148 TimerGuard &operator=(const TimerGuard &) = delete;
149 TimerGuard &operator=(TimerGuard &&) = delete;
150
155 ~TimerGuard();
156
158 void Pause();
159
161 void Resume();
162
163 private:
164 Timer *const timer_;
165 const bool enabled_{false};
166};
167
168} // namespace delpi
Simple timer class to evaluate the performance of the software.
Definition Timer.h:26
void Resume()
Resume the timer.
Definition Timer.cpp:38
duration elapsed_
Elapsed time so far. This doesn't include the current fragment if it is running.
Definition Timer.h:73
void Pause()
Pause the timer.
Definition Timer.cpp:30
void Start()
Start the timer.
Definition Timer.cpp:22
time_point last_start_
Last time_point when the timer is started or resumed.
Definition Timer.h:72
bool running_
Whether the timer is running or not.
Definition Timer.h:71
void Pause()
Pause the guarded timer object.
Definition Timer.cpp:83
const bool enabled_
Whether the timer is enabled.
Definition Timer.h:165
TimerGuard(Timer *timer, bool enabled, bool start_timer=true)
Construct a new TimeGuard object.
Definition Timer.cpp:74
void Resume()
Resume the guarded timer object.
Definition Timer.cpp:87
Timer *const timer_
The timer to be guarded.
Definition Timer.h:164
Timer class using the a steady clock.
Definition Timer.h:95
Timer class using the user_clock.
Definition Timer.h:112
Global namespace for the delpi library.
Structure that will hold the user clock data.
Definition Timer.h:101