mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
stopwatch.h
Go to the documentation of this file.
1 #ifndef MF_UTILITY_STOPWATCH_H_
2 #define MF_UTILITY_STOPWATCH_H_
3 
4 #include "../common.h"
5 #include <chrono>
6 
7 namespace mf {
8 
10 
11 class stopwatch {
12 public:
13  using clock_type = std::chrono::high_resolution_clock;
14  using time_point_type = typename clock_type::time_point;
15  using duration_type = typename clock_type::duration;
16 
17 private:
18  duration_type total_duration_ = duration_type::zero();
19  time_point_type start_time_;
20  bool running_ = false;
21 
22 public:
24  void reset() {
25  total_duration_ = duration_type::zero();
26  running_ = false;
27  }
28 
30  void start() {
31  start_time_ = clock_type::now();
32  running_ = true;
33  }
34 
36  void stop() {
37  if(! running_) return;
38  running_ = false;
39  time_point_type end_time = clock_type::now();
40  duration_type duration = end_time - start_time_;
41  total_duration_ += duration;
42  start_time_ = end_time;
43  }
44 
46 
48  MF_ASSERT(! running_);
49  return total_duration_;
50  }
51 };
52 
53 }
54 
55 #endif
duration_type total_duration() const
Get total duration.
Definition: stopwatch.h:47
void stop()
Stop the stopwatch, and add duration since last start() call to total duration.
Definition: stopwatch.h:36
typename clock_type::duration duration_type
Definition: stopwatch.h:15
void reset()
Reset accumulated duration and stop.
Definition: stopwatch.h:24
Stop watch which measures total time passed between start() and stop() calls.
Definition: stopwatch.h:11
std::chrono::high_resolution_clock clock_type
Definition: stopwatch.h:13
typename clock_type::time_point time_point_type
Definition: stopwatch.h:14
void start()
Start the stopwatch.
Definition: stopwatch.h:30
#define MF_ASSERT(condition)
Definition: common.h:24