HIBF 1.0.0-rc.1
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2026, Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2026, Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <algorithm> // for max
13#include <atomic> // for atomic, memory_order_relaxed
14#include <cassert> // for assert
15#include <chrono> // for duration, time_point, operator-, operator<=>, steady_clock
16#include <compare> // for operator>=, _CmpUnspecifiedParam, strong_ordering
17#include <concepts> // for same_as
18#include <cstdint> // for uint64_t
19#include <ratio> // for ratio
20
21#include <hibf/platform.hpp>
22
23namespace seqan::hibf
24{
25
26class concurrent_timer;
27
32{
33private:
35
36 steady_clock_t::time_point start_point{std::chrono::time_point<steady_clock_t>::max()};
37 steady_clock_t::time_point stop_point{};
38
39 steady_clock_t::rep ticks{};
40 steady_clock_t::rep max{};
41 uint64_t count{};
42
43 friend class concurrent_timer;
44
45public:
49 serial_timer() = default;
50 serial_timer(serial_timer const &) = default;
51 serial_timer & operator=(serial_timer const &) = default;
52 serial_timer(serial_timer &&) = default;
54 ~serial_timer() = default;
55
57
62 void start()
63 {
64 start_point = steady_clock_t::now();
65 }
66
71 void stop()
72 {
73 stop_point = steady_clock_t::now();
74 assert(stop_point >= start_point);
75 steady_clock_t::rep duration = (stop_point - start_point).count();
76
77 ticks += duration;
78 max = std::max(max, duration);
79 ++count;
80 }
81
83 template <typename timer_t>
85 void operator+=(timer_t const & other)
86 {
87 steady_clock_t::rep ticks_to_add{};
89 ticks_to_add = other.ticks.load();
90 else
91 ticks_to_add = other.ticks;
92
93 ticks += ticks_to_add;
94 max = std::max(max, ticks_to_add);
95 ++count;
96 }
98
103 double in_seconds() const
104 {
105 return std::chrono::duration<double>(steady_clock_t::duration{ticks}).count();
106 }
107
114 double max_in_seconds() const
115 {
116 return std::chrono::duration<double>(steady_clock_t::duration{max}).count();
117 }
118
128 double avg_in_seconds() const
129 {
130 assert(count > 0u);
131 return in_seconds() / count;
132 }
134
139 constexpr bool operator==(serial_timer const &) const
140 {
141 return true;
142 }
143
145 constexpr bool operator==(concurrent_timer const &) const
146 {
147 return true;
148 }
150};
151
156{
157private:
159
160 alignas(64) std::atomic<steady_clock_t::rep> ticks{};
161
162 steady_clock_t::time_point start_point{std::chrono::time_point<steady_clock_t>::max()};
163 steady_clock_t::time_point stop_point{};
164
165 alignas(64) std::atomic<steady_clock_t::rep> max{};
166 alignas(64) std::atomic<uint64_t> count{};
167
168 friend class serial_timer;
169
170 void update_max(steady_clock_t::rep const value)
171 {
172 for (steady_clock_t::rep previous_value = max;
173 previous_value < value && !max.compare_exchange_weak(previous_value, value, std::memory_order_relaxed);)
174 ;
175 }
176
177public:
182 concurrent_timer() = default;
185 ticks{other.ticks.load()},
186 start_point{other.start_point},
187 stop_point{other.stop_point},
188 max{other.max.load()},
189 count{other.count.load()}
190 {}
193 {
194 ticks = other.ticks.load();
195 start_point = other.start_point;
196 stop_point = other.stop_point;
197 max = other.max.load();
198 count = other.count.load();
199 return *this;
200 }
203 ticks{other.ticks.load()},
204 start_point{other.start_point},
205 stop_point{other.stop_point},
206 max{other.max.load()},
207 count{other.count.load()}
208 {}
211 {
212 ticks = other.ticks.load();
213 start_point = other.start_point;
214 stop_point = other.stop_point;
215 max = other.max.load();
216 count = other.count.load();
217 return *this;
218 }
220 ~concurrent_timer() = default;
222
227 void start()
228 {
229 start_point = steady_clock_t::now();
230 }
231
235 void stop()
236 {
237 stop_point = steady_clock_t::now();
238 assert(stop_point >= start_point);
239 steady_clock_t::rep duration = (stop_point - start_point).count();
240
241 ticks += duration;
242 update_max(duration);
243 ++count;
244 }
245
249 template <typename timer_t>
251 void operator+=(timer_t const & other)
252 {
253 ticks += other.ticks;
254 update_max(other.ticks);
255 ++count;
256 }
258
263 double in_seconds() const
264 {
265 return std::chrono::duration<double>(steady_clock_t::duration{ticks.load()}).count();
266 }
267
269 double max_in_seconds() const
270 {
271 return std::chrono::duration<double>(steady_clock_t::duration{max.load()}).count();
272 }
273
275 double avg_in_seconds() const
276 {
277 assert(count.load() > 0u);
278 return in_seconds() / count.load();
279 }
280
285 constexpr bool operator==(serial_timer const &) const
286 {
287 return true;
288 }
289
291 constexpr bool operator==(concurrent_timer const &) const
292 {
293 return true;
294 }
296};
297
298} // namespace seqan::hibf
A timer with a thread-safe operator+=().
Definition timer.hpp:156
void stop()
Stops the timer.
Definition timer.hpp:235
~concurrent_timer()=default
Defaulted.
double avg_in_seconds() const
Returns the average measured time interval in seconds.
Definition timer.hpp:275
constexpr bool operator==(serial_timer const &) const
Two timer are always equal.
Definition timer.hpp:285
constexpr bool operator==(concurrent_timer const &) const
Two timer are always equal.
Definition timer.hpp:291
concurrent_timer()=default
Defaulted.
double in_seconds() const
Returns the measured time in seconds.
Definition timer.hpp:263
concurrent_timer(concurrent_timer &&other) noexcept
Defaulted.
Definition timer.hpp:202
concurrent_timer & operator=(concurrent_timer &&other) noexcept
Defaulted.
Definition timer.hpp:210
concurrent_timer & operator=(concurrent_timer const &other)
Defaulted.
Definition timer.hpp:192
void start()
Starts the timer.
Definition timer.hpp:227
double max_in_seconds() const
Returns the maximum measured time interval in seconds.
Definition timer.hpp:269
concurrent_timer(concurrent_timer const &other)
Defaulted.
Definition timer.hpp:184
A timer.
Definition timer.hpp:32
constexpr bool operator==(serial_timer const &) const
Two timer are always equal.
Definition timer.hpp:139
serial_timer(serial_timer &&)=default
Defaulted.
double max_in_seconds() const
Returns the maximum measured time interval in seconds.
Definition timer.hpp:114
serial_timer()=default
Defaulted.
serial_timer & operator=(serial_timer &&)=default
Defaulted.
serial_timer & operator=(serial_timer const &)=default
Defaulted.
constexpr bool operator==(concurrent_timer const &) const
Two timer are always equal.
Definition timer.hpp:145
double avg_in_seconds() const
Returns the average measured time interval in seconds.
Definition timer.hpp:128
~serial_timer()=default
Defaulted.
void stop()
Stops the timer.
Definition timer.hpp:71
double in_seconds() const
Returns the measured time in seconds.
Definition timer.hpp:103
void start()
Starts the timer.
Definition timer.hpp:62
serial_timer(serial_timer const &)=default
Defaulted.
T compare_exchange_weak(T... args)
T is_base_of_v
T load(T... args)
Provides platform and dependency checks.