dReal4
timer.h
1 #pragma once
2 
3 #include <chrono>
4 #include <iostream>
5 #include <type_traits>
6 
7 namespace dreal {
8 
9 /// Simple timer class to profile performance.
10 class Timer {
11  public:
12  // Use high_resolution clock if it's steady. Otherwise use steady_clock.
13  using clock = std::conditional<std::chrono::high_resolution_clock::is_steady,
14  std::chrono::high_resolution_clock,
15  std::chrono::steady_clock>::type;
16  Timer();
17 
18  /// Starts the timer.
19  void start();
20 
21  /// Pauses the timer.
22  void pause();
23 
24  /// Resumes the timer.
25  void resume();
26 
27  /// Checks if the timer is running.
28  bool is_running() const;
29 
30  /// Returns the elapsed time as duration.
31  clock::duration elapsed() const;
32 
33  /// Returns the elapsed time in secionds.
34  std::chrono::duration<double>::rep seconds() const;
35 
36  private:
37  // Whether the timer is running or not.
38  bool running_{false};
39 
40  // Last time_point when the timer is started or resumed.
41  clock::time_point last_start_{};
42 
43  // Elapsed time so far. This doesn't include the current fragment if
44  // it is running.
45  clock::duration elapsed_{};
46 };
47 
48 std::ostream& operator<<(std::ostream& os, const Timer& timer);
49 
50 /// Pauses the passed timer object when the guard object is destructed
51 /// (e.g. going out of scope).
52 class TimerGuard {
53  public:
54  /// Constructs the timer guard object with @p timer.
55  ///
56  /// If @p enabled is false, this class does not do anything.
57  /// If @p start_timer is true, starts the @p timer in the
58  /// constructor. Otherwise, it does not start it and a user has to
59  /// call `resume()` to start it.
60  TimerGuard(Timer* timer, bool enabled, bool start_timer = true);
61 
62  TimerGuard(const TimerGuard&) = delete;
63  TimerGuard(TimerGuard&&) = delete;
64  TimerGuard& operator=(const TimerGuard&) = delete;
65  TimerGuard& operator=(TimerGuard&&) = delete;
66 
67 
68  /// Destructs the timer guard object. It pauses the embedded timer object.
69  ~TimerGuard();
70 
71  /// Pauses the embedded timer object.
72  void pause();
73 
74  /// Resumes the embedded timer object.
75  void resume();
76 
77  private:
78  Timer* const timer_;
79  const bool enabled_{false};
80 };
81 
82 } // namespace dreal
void start()
Starts the timer.
Definition: timer.cc:9
std::chrono::duration< double >::rep seconds() const
Returns the elapsed time in secionds.
Definition: timer.cc:39
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
bool is_running() const
Checks if the timer is running.
Definition: timer.cc:29
clock::duration elapsed() const
Returns the elapsed time as duration.
Definition: timer.cc:31
void pause()
Pauses the timer.
Definition: timer.cc:15
void resume()
Resumes the timer.
Definition: timer.cc:22
Pauses the passed timer object when the guard object is destructed (e.g.
Definition: timer.h:52
Simple timer class to profile performance.
Definition: timer.h:10