10#include "TimeLogger.h"
39template <
typename T = std::chrono::high_resolution_clock>
51 Timer(std::optional<std::string> task = std::nullopt) : _task(std::move(task))
64 : _task(std::exchange(timer._task, std::nullopt)), _acc(timer._acc),
65 _start_time(std::exchange(timer._start_time, std::nullopt))
73 if (_start_time.has_value() and _task.has_value())
75 _acc += T::now() - *_start_time;
95 _task = std::exchange(timer._task, std::nullopt);
97 _start_time = std::exchange(timer._start_time, std::nullopt);
104 _acc = T::duration::zero();
105 _start_time = T::now();
113 template <
typename Period = std::ratio<1>>
114 std::chrono::duration<double, Period>
elapsed()
const
116 if (_start_time.has_value())
117 return T::now() - *_start_time + _acc;
127 template <
typename Period = std::ratio<1>>
128 std::chrono::duration<double, Period>
stop()
130 if (_start_time.has_value())
132 _acc += T::now() - *_start_time;
133 _start_time = std::nullopt;
144 if (!_start_time.has_value())
145 _start_time = T::now();
157 if (_start_time.has_value())
158 throw std::runtime_error(
"Timer must be stopped before flushing.");
160 if (_task.has_value())
163 _task = std::nullopt;
169 std::optional<std::string> _task;
172 typename T::duration _acc = T::duration::zero();
175 std::optional<typename T::time_point> _start_time = T::now();
void register_timing(std::string_view task, std::chrono::duration< double, std::ratio< 1 > > wall)
Register timing (for later summary).
Definition TimeLogger.cpp:25
static TimeLogger & instance()
Singleton access.
Definition TimeLogger.cpp:18
Timer for measuring and logging elapsed time durations.
Definition Timer.h:41
std::chrono::duration< double, Period > stop()
Stop timer and return elapsed time.
Definition Timer.h:128
void resume()
Resume a stopped timer.
Definition Timer.h:142
~Timer()
Definition Timer.h:71
void start()
Reset elapsed time and (re-)start timer.
Definition Timer.h:102
std::chrono::duration< double, Period > elapsed() const
Elapsed time since time has been started.
Definition Timer.h:114
Timer(Timer &&timer) noexcept
Move constructor.
Definition Timer.h:63
Timer(std::optional< std::string > task=std::nullopt)
Create and start timer.
Definition Timer.h:51
Timer & operator=(Timer &&timer) noexcept
Move assignment.
Definition Timer.h:93
void flush()
Flush timer duration to the logger.
Definition Timer.h:155
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8