dlinear  0.0.1
Delta-complete SMT solver for linear programming
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 dlinear {
16
17struct user_clock;
18
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<std::chrono::high_resolution_clock::is_steady,
91 std::chrono::high_resolution_clock, std::chrono::steady_clock>::type;
92
93extern template class TimerBase<chosen_steady_clock>;
96
97struct user_clock { // Implements the Clock interface of std::chrono
98 typedef uint64_t rep;
99 typedef std::micro period;
100 typedef std::chrono::duration<rep, period> duration;
101 typedef std::chrono::time_point<user_clock> time_point;
102 const bool is_steady = false; // Not sure how this should be interpreted here
103 static time_point now();
104};
105
106extern template class TimerBase<user_clock>;
109
130 public:
141 TimerGuard(Timer *timer, bool enabled, bool start_timer = true);
142
143 TimerGuard(const TimerGuard &) = delete;
144 TimerGuard(TimerGuard &&) = delete;
145 TimerGuard &operator=(const TimerGuard &) = delete;
146 TimerGuard &operator=(TimerGuard &&) = delete;
147
152 ~TimerGuard();
153
155 void Pause();
156
158 void Resume();
159
160 private:
161 Timer *const timer_;
162 const bool enabled_{false};
163};
164
165} // namespace dlinear
Simple timer class to evaluate the performance of the software.
Definition Timer.h:26
TimerBase()
Construct a new TimerBase object.
Definition Timer.cpp:18
duration elapsed_
Elapsed time so far. This doesn't include the current fragment if it is running.
Definition Timer.h:73
duration elapsed() const
Get read-only access to the duration of elapsed time of the timer.
Definition Timer.cpp:50
time_point last_start_
Last time_point when the timer is started or resumed.
Definition Timer.h:72
bool is_running() const
Check whether the timer is running.
Definition Timer.cpp:45
void Resume()
Resume the timer.
Definition Timer.cpp:37
time_point now() const
Get read-only access to the current instant of the timer.
Definition Timer.h:68
void Start()
Start the timer.
Definition Timer.cpp:21
void Pause()
Pause the timer.
Definition Timer.cpp:29
bool running_
Whether the timer is running or not.
Definition Timer.h:71
std::chrono::duration< double >::rep seconds() const
Get read-only access to the number elapsed seconds of the timer.
Definition Timer.cpp:56
The TimeGuard wraps a timer object and pauses it when the guard object is destructed.
Definition Timer.h:129
Timer *const timer_
The timer to be guarded.
Definition Timer.h:161
Timer class using the a steady clock.
Definition Timer.h:95
Timer class using the user_clock.
Definition Timer.h:108
Global namespace for the dlinear library.