LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
timer.h
1
10// clang-format off
11//
12// Boost Software License - Version 1.0 - August 17th, 2003
13//
14// Permission is hereby granted, free of charge, to any person or organization
15// obtaining a copy of the software and accompanying documentation covered by
16// this license (the "Software") to use, reproduce, display, distribute,
17// execute, and transmit the Software, and to prepare derivative works of the
18// Software, and to permit third-parties to whom the Software is furnished to
19// do so, all subject to the following:
20//
21// The copyright notices in the Software and this entire statement, including
22// the above license grant, this restriction and the following disclaimer,
23// must be included in all copies of the Software, in whole or in part, and
24// all derivative works of the Software, unless such copies or derivative
25// works are solely in the form of machine-executable object code generated by
26// a source language processor.
27//
28// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
31// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
32// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
33// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
34// DEALINGS IN THE SOFTWARE.
35//
36// clang-format on
37
38#ifndef __529e391f2915453388028b848b3dcbd6
39#define __529e391f2915453388028b848b3dcbd6
40
41#include <spdlog/logger.h>
42#include <chrono>
43#include <cstdint>
44#include <memory>
45#include <string>
46#include <variant>
47
48#include "lf_assert.h"
49
50namespace lf::base {
51
63class Timer {
64 public:
69 struct cpu_times {
76 std::chrono::nanoseconds wall;
84 std::chrono::nanoseconds user;
92 std::chrono::nanoseconds system;
93 };
94
100 explicit Timer(bool start = true) noexcept : times_() {
101 if (start) {
102 Start();
103 } else {
104 times_ =
105 cpu_times{std::chrono::nanoseconds(0), std::chrono::nanoseconds(0),
106 std::chrono::nanoseconds(0)};
107 is_stopped_ = true;
108 }
109 }
110
114 [[nodiscard]] bool IsStopped() const noexcept { return is_stopped_; }
115
119 [[nodiscard]] cpu_times Elapsed() const noexcept;
120
140 [[nodiscard]] std::string Format(
141 std::string_view format = kDefaultFormat) const;
142
147 void Start() noexcept;
148
152 void Stop() noexcept;
153
158 void Resume() noexcept;
159
163 inline static std::string kDefaultFormat =
164 "{w:.6}s wall, {u:.6}s user + {s:.6}s system = {t:.6}s CPU ({p:.3}%)";
165
166 private:
167 cpu_times times_;
168 bool is_stopped_;
169};
170
181class AutoTimer {
182 public:
189 explicit AutoTimer(std::string format = Timer::kDefaultFormat);
190
198 explicit AutoTimer(std::ostream& stream,
199 std::string format = Timer::kDefaultFormat);
200
210 explicit AutoTimer(
211 std::shared_ptr<spdlog::logger> logger,
212 spdlog::level::level_enum level = spdlog::level::level_enum::info,
213 std::string format = Timer::kDefaultFormat);
214
215 AutoTimer(const AutoTimer&) = default;
216 AutoTimer(AutoTimer&&) = default;
217 AutoTimer& operator=(const AutoTimer&) = default;
218 AutoTimer& operator=(AutoTimer&&) = default;
219
223 ~AutoTimer();
224
231 [[nodiscard]] std::ostream& ostream() const;
232
238 [[nodiscard]] const std::shared_ptr<spdlog::logger>& logger() const;
239
243 [[nodiscard]] const std::string& FormatString() const;
244
253 void Report();
254
259 [[nodiscard]] Timer::cpu_times Elapsed() const noexcept;
260
262 [[nodiscard]] std::string Format(
263 std::string_view format = Timer::kDefaultFormat) const;
264
265 private:
266 Timer timer_;
267 std::string format_;
268
269 // ostream is stored as pointer so assignment operator works.
270 std::variant<std::ostream*, std::pair<std::shared_ptr<spdlog::logger>,
271 spdlog::level::level_enum>>
272 output_;
273};
274
275} // namespace lf::base
276
277#endif // __529e391f2915453388028b848b3dcbd6
Timer class to measure time.
Definition: timer.h:63
bool is_stopped_
Definition: timer.h:168
std::string Format(std::string_view format=kDefaultFormat) const
Return the number of elapsed seconds of the wall clock time, user time and system time as a formatted...
Definition: timer.cc:100
void Stop() noexcept
stop the timer, stop counting the elapsed time.
Definition: timer.cc:119
void Resume() noexcept
Resume the timer, i.e. start counting from where we stopped the last time.
Definition: timer.cc:132
cpu_times Elapsed() const noexcept
Elapsed time since timer start, doesn't stop the timer.
Definition: timer.cc:86
static std::string kDefaultFormat
Default format string that is used by Format()
Definition: timer.h:163
bool IsStopped() const noexcept
Is the timer currently stopped?
Definition: timer.h:114
cpu_times times_
Definition: timer.h:167
Timer(bool start=true) noexcept
Construct a new timer and optionally start it immediately.
Definition: timer.h:100
void Start() noexcept
(Re)starts the timer, i.e. the timer starts counting from 0 when this method is called.
Definition: timer.cc:114
Contains basic functionality that is used by other parts of LehrFEM++.
Definition: base.h:15
Packages the elapsed wall clock time, user process CPU time, and system process CPU time.
Definition: timer.h:69
std::chrono::nanoseconds system
Elapsed System time in nano-seconds.
Definition: timer.h:92
std::chrono::nanoseconds user
Elapsed User time in nano-seconds.
Definition: timer.h:84
std::chrono::nanoseconds wall
Elapsed WallClock time in nano-seconds.
Definition: timer.h:76