Bembel
Stopwatch.hpp
1 // This file is part of Bembel, the higher order C++ boundary element library.
2 //
3 // Copyright (C) 2022 see <http://www.bembel.eu>
4 //
5 // It was written as part of a cooperation of J. Doelz, H. Harbrecht, S. Kurz,
6 // M. Multerer, S. Schoeps, and F. Wolf at Technische Universitaet Darmstadt,
7 // Universitaet Basel, and Universita della Svizzera italiana, Lugano. This
8 // source code is subject to the GNU General Public License version 3 and
9 // provided WITHOUT ANY WARRANTY, see <http://www.bembel.eu> for further
10 // information.
11 #ifndef BEMBEL_SRC_IO_STOPWATCH_HPP_
12 #define BEMBEL_SRC_IO_STOPWATCH_HPP_
13 
14 namespace Bembel {
15 
16 namespace IO {
27 class Stopwatch {
28  std::chrono::time_point<std::chrono::high_resolution_clock> p_;
29  double sum_;
30  std::vector<double> durations_;
31  bool has_been_started_;
32  bool has_lapped_;
33 
34  public:
35  inline Stopwatch() {
36  has_been_started_ = false;
37  has_lapped_ = false;
38  }
39 
40  void tic(void) { p_ = std::chrono::high_resolution_clock::now(); }
41  double toc(void) {
42  double dtime = std::chrono::duration<double, std::ratio<1, 1>>(
43  std::chrono::high_resolution_clock::now() - p_)
44  .count();
45  return dtime;
46  }
47  inline void start() {
48  durations_ = {};
49  assert(has_been_started_ == false && "Stopwatch already running!");
50  has_been_started_ = true;
51  p_ = std::chrono::high_resolution_clock::now();
52  }
53  inline double lap() {
54  double out = std::chrono::duration<double, std::ratio<1, 1>>(
55  std::chrono::high_resolution_clock::now() - p_)
56  .count();
57  durations_.push_back(out);
58  assert(has_been_started_ == true && "Stopwatch must be started first!");
59  has_lapped_ = true;
60  p_ = std::chrono::high_resolution_clock::now();
61  return out;
62  }
63  inline double stop() {
64  double out = std::chrono::duration<double, std::ratio<1, 1>>(
65  std::chrono::high_resolution_clock::now() - p_)
66  .count();
67  assert(has_been_started_ == true && "Stopwatch must be started first!");
68  if (has_lapped_) {
69  has_lapped_ = false;
70  has_been_started_ = false;
71  return std::accumulate(durations_.begin(), durations_.end(), 0.0);
72  }
73  return out;
74  }
75  inline std::vector<double> get_data() { return durations_; }
76 };
77 } // namespace IO
78 } // namespace Bembel
79 #endif // BEMBEL_SRC_IO_STOPWATCH_HPP_
A simple class for benchmarking.
Definition: Stopwatch.hpp:27
Routines for the evalutation of pointwise errors.
Definition: AnsatzSpace.hpp:14