faunus
timers.h
1 #pragma once
2 #include <chrono>
3 #include <iostream>
4 
5 namespace Faunus {
13 template <typename Tunit = std::chrono::microseconds> class TimeRelativeOfTotal
14 {
15  private:
16  Tunit delta;
17  std::chrono::steady_clock::time_point t0, tx;
18 
19  public:
21  : delta(0)
22  {
23  t0 = std::chrono::steady_clock::now();
24  }
25 
26  operator bool() const { return delta.count() != 0 ? true : false; }
27 
28  void start() { tx = std::chrono::steady_clock::now(); }
29 
30  void stop()
31  {
32  delta += std::chrono::duration_cast<Tunit>(std::chrono::steady_clock::now() - tx);
33  }
34 
35  double result() const
36  {
37  auto now = std::chrono::steady_clock::now();
38  auto total = std::chrono::duration_cast<Tunit>(now - t0);
39  return delta.count() / double(total.count());
40  }
41 };
42 
58 class Stopwatch
59 {
60  using clock = std::chrono::high_resolution_clock;
61  std::chrono::time_point<clock> starting_time, ending_time;
62 
63  public:
64  inline Stopwatch() { start(); }
65 
66  inline void start() { starting_time = clock::now(); }
67 
68  inline void stop() { ending_time = clock::now(); }
69 };
70 
71 } // namespace Faunus
Timer for measuring relative time consumption.
Definition: timers.h:13
Simple struct to measure duration of code.
Definition: timers.h:58
Cell list class templates.
Definition: actions.cpp:11