LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
Loading...
Searching...
No Matches
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 INCG_529e391f2915453388028b848b3dcbd6
39#define INCG_529e391f2915453388028b848b3dcbd6
40
41#include <spdlog/logger.h>
42
43#include <chrono>
44#include <cstdint>
45#include <memory>
46#include <string>
47#include <variant>
48
49#include "lf_assert.h"
50
51namespace lf::base {
52
64class Timer {
65 public:
70 struct cpu_times {
77 std::chrono::nanoseconds wall;
85 std::chrono::nanoseconds user;
93 std::chrono::nanoseconds system;
94 };
95
101 explicit Timer(bool start = true) noexcept : times_() {
102 if (start) {
103 Start();
104 } else {
105 times_ =
106 cpu_times{std::chrono::nanoseconds(0), std::chrono::nanoseconds(0),
107 std::chrono::nanoseconds(0)};
108 is_stopped_ = true;
109 }
110 }
111
115 [[nodiscard]] bool IsStopped() const noexcept { return is_stopped_; }
116
120 [[nodiscard]] cpu_times Elapsed() const noexcept;
121
141 [[nodiscard]] std::string Format(
142 std::string_view format = kDefaultFormat) const;
143
148 void Start() noexcept;
149
153 void Stop() noexcept;
154
159 void Resume() noexcept;
160
164 inline static std::string kDefaultFormat =
165 "{w:.6}s wall, {u:.6}s user + {s:.6}s system = {t:.6}s CPU ({p:.3}%)";
166
167 private:
168 cpu_times times_;
169 bool is_stopped_;
170};
171
182class AutoTimer {
183 public:
190 explicit AutoTimer(std::string format = Timer::kDefaultFormat);
191
199 explicit AutoTimer(std::ostream& stream,
200 std::string format = Timer::kDefaultFormat);
201
211 explicit AutoTimer(
212 std::shared_ptr<spdlog::logger> logger,
213 spdlog::level::level_enum level = spdlog::level::level_enum::info,
214 std::string format = Timer::kDefaultFormat);
215
216 AutoTimer(const AutoTimer&) = default;
217 AutoTimer(AutoTimer&&) = default;
218 AutoTimer& operator=(const AutoTimer&) = default;
219 AutoTimer& operator=(AutoTimer&&) = default;
220
224 ~AutoTimer();
225
232 [[nodiscard]] std::ostream& ostream() const;
233
239 [[nodiscard]] const std::shared_ptr<spdlog::logger>& logger() const;
240
244 [[nodiscard]] const std::string& FormatString() const;
245
254 void Report();
255
260 [[nodiscard]] Timer::cpu_times Elapsed() const noexcept;
261
263 [[nodiscard]] std::string Format(
264 std::string_view format = Timer::kDefaultFormat) const;
265
266 private:
267 Timer timer_;
268 std::string format_;
269
270 // ostream is stored as pointer so assignment operator works.
271 std::variant<std::ostream*, std::pair<std::shared_ptr<spdlog::logger>,
272 spdlog::level::level_enum>>
273 output_;
274};
275
276} // namespace lf::base
277
278#endif // INCG_529e391f2915453388028b848b3dcbd6
Timer class to measure time.
Definition timer.h:64
bool is_stopped_
Definition timer.h:169
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:164
bool IsStopped() const noexcept
Is the timer currently stopped?
Definition timer.h:115
cpu_times times_
Definition timer.h:168
Timer(bool start=true) noexcept
Construct a new timer and optionally start it immediately.
Definition timer.h:101
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:70
std::chrono::nanoseconds system
Elapsed System time in nano-seconds.
Definition timer.h:93
std::chrono::nanoseconds user
Elapsed User time in nano-seconds.
Definition timer.h:85
std::chrono::nanoseconds wall
Elapsed WallClock time in nano-seconds.
Definition timer.h:77