cpu_timer.h
Go to the documentation of this file.
1 #ifndef cetlib_cpu_timer_h
2 #define cetlib_cpu_timer_h
3 
4 // ======================================================================
5 //
6 // cpu_timer: Measure the cpu and wallclock elapsed times
7 //
8 // ======================================================================
9 
10 #include <sys/time.h>
11 
12 namespace cet {
13  class cpu_timer;
14 }
15 
16 // ======================================================================
17 
19 public:
20  cpu_timer() = default;
21 
22  // Allow move
23  cpu_timer(cpu_timer&&) = default;
24  cpu_timer& operator=(cpu_timer&&) = default;
25 
26  // Disable copy
27  cpu_timer(cpu_timer const&) = delete;
28  cpu_timer& operator=(cpu_timer const&) = delete;
29 
30  // --- accessors:
31  bool is_running() const;
32  bool is_stopped() const;
33  double elapsed_real_time() const;
34  double elapsed_cpu_time() const;
35  double accumulated_real_time() const;
36  double accumulated_cpu_time() const;
37 
38  inline double
39  realTime() const
40  {
41  return accumulated_real_time();
42  }
43  inline double
44  cpuTime() const
45  {
46  return accumulated_cpu_time();
47  }
48 
49  // --- mutators:
50  void start();
51  void stop();
52  void reset();
53 
54 private:
55  // --- state:
56  bool is_running_{false};
57  timeval start_real_time_{0, 0};
58  timeval start_cpu_time_{0, 0};
61 
62 }; // cpu_timer
63 
64 // ======================================================================
65 
66 inline bool
68 {
69  return is_running_;
70 }
71 
72 inline bool
74 {
75  return !is_running();
76 }
77 
78 // ======================================================================
79 
80 #endif /* cetlib_cpu_timer_h */
81 
82 // Local variables:
83 // mode: c++
84 // End:
cpu_timer & operator=(cpu_timer &&)=default
double accumulated_cpu_time_
Definition: cpu_timer.h:60
cpu_timer()=default
timeval start_cpu_time_
Definition: cpu_timer.h:58
timeval start_real_time_
Definition: cpu_timer.h:57
bool is_running() const
Definition: cpu_timer.h:67
bool is_running_
Definition: cpu_timer.h:56
double realTime() const
Definition: cpu_timer.h:39
double accumulated_cpu_time() const
Definition: cpu_timer.cc:73
double elapsed_cpu_time() const
Definition: cpu_timer.cc:60
double accumulated_real_time_
Definition: cpu_timer.h:59
bool is_stopped() const
Definition: cpu_timer.h:73
double accumulated_real_time() const
Definition: cpu_timer.cc:66
double elapsed_real_time() const
Definition: cpu_timer.cc:54
void start()
Definition: cpu_timer.cc:83
double cpuTime() const
Definition: cpu_timer.h:44