DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
Timer.h
1// Copyright (C) 2008-2015 Anders Logg, Jan Blechta, Paul T. Kühner and Garth N.
2// Wells
3//
4// This file is part of DOLFINx (https://www.fenicsproject.org)
5//
6// SPDX-License-Identifier: LGPL-3.0-or-later
7
8#pragma once
9
10#include "TimeLogger.h"
11#include <chrono>
12#include <optional>
13#include <stdexcept>
14#include <string>
15#include <utility>
16
17namespace dolfinx::common
18{
39template <typename T = std::chrono::high_resolution_clock>
40class Timer
41{
42public:
51 Timer(std::optional<std::string> task = std::nullopt) : _task(std::move(task))
52 {
53 }
54
55 // Copy constructor (deleted). Both copies would register `_task` when
56 // destroyed, counting it twice.
57 Timer(const Timer& timer) = delete;
58
63 Timer(Timer&& timer) noexcept
64 : _task(std::exchange(timer._task, std::nullopt)), _acc(timer._acc),
65 _start_time(std::exchange(timer._start_time, std::nullopt))
66 {
67 }
68
72 {
73 if (_start_time.has_value() and _task.has_value())
74 {
75 _acc += T::now() - *_start_time;
76 try
77 {
79 }
80 catch (...)
81 {
82 // Destructor is noexcept; discard the timing rather than terminate.
83 }
84 }
85 }
86
87 // Copy assignment (deleted)
88 Timer& operator=(const Timer& timer) = delete;
89
93 Timer& operator=(Timer&& timer) noexcept
94 {
95 _task = std::exchange(timer._task, std::nullopt);
96 _acc = timer._acc;
97 _start_time = std::exchange(timer._start_time, std::nullopt);
98 return *this;
99 }
100
102 void start()
103 {
104 _acc = T::duration::zero();
105 _start_time = T::now();
106 }
107
113 template <typename Period = std::ratio<1>>
114 std::chrono::duration<double, Period> elapsed() const
115 {
116 if (_start_time.has_value()) // Timer is running
117 return T::now() - *_start_time + _acc;
118 else // Timer is stopped
119 return _acc;
120 }
121
127 template <typename Period = std::ratio<1>>
128 std::chrono::duration<double, Period> stop()
129 {
130 if (_start_time.has_value()) // Timer is running
131 {
132 _acc += T::now() - *_start_time;
133 _start_time = std::nullopt;
134 }
135
136 return _acc;
137 }
138
142 void resume()
143 {
144 if (!_start_time.has_value())
145 _start_time = T::now();
146 }
147
155 void flush()
156 {
157 if (_start_time.has_value())
158 throw std::runtime_error("Timer must be stopped before flushing.");
159
160 if (_task.has_value())
161 {
163 _task = std::nullopt;
164 }
165 }
166
167private:
168 // Name of task to register in logger
169 std::optional<std::string> _task;
170
171 // Elapsed time offset
172 typename T::duration _acc = T::duration::zero();
173
174 // Store start time (std::nullopt if timer has been stopped)
175 std::optional<typename T::time_point> _start_time = T::now();
176};
177} // namespace dolfinx::common
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