CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FastTimer.cc
Go to the documentation of this file.
1 // C++ headers
2 #include <chrono>
3 #include <iostream>
4 #include <vector>
5 #include <string>
6 
8 
10  m_start(),
11  m_stop(),
12  m_duration(Duration::zero()),
13  m_state(State::kStopped)
14 { }
15 
16 // start the timer - only if it was not running
18  if (m_state == State::kStopped) {
19  m_start = Clock::now();
20  m_stop = Clock::time_point();
21  m_duration = Duration::zero();
23  } else {
24  std::cerr << "attempting to start a " << describe() << " timer" << std::endl;
25  }
26 }
27 
28 // stop the timer - only if it was running
30  if (m_state == State::kRunning) {
31  m_stop = Clock::now();
32  m_duration += std::chrono::duration_cast<Duration>(m_stop - m_start);
34  } else {
35  std::cerr << "attempting to stop a " << describe() << " timer" << std::endl;
36  }
37 }
38 
39 // pause the timer - only if it was running
41  if (m_state == State::kRunning) {
42  m_stop = Clock::now();
43  m_duration += std::chrono::duration_cast<Duration>(m_stop - m_start);
45  } else {
46  std::cerr << "attempting to pause a " << describe() << " timer" << std::endl;
47  }
48 }
49 
50 // resume the timer - only if it was not running
52  if (m_state == State::kPaused) {
53  m_start = Clock::now();
54  m_stop = Clock::time_point();
56  } else {
57  std::cerr << "attempting to resume a " << describe() << " timer" << std::endl;
58  }
59 }
60 
61 // reset the timer
63  m_start = Clock::time_point();
64  m_stop = Clock::time_point();
65  m_duration = Duration::zero();
67 }
68 
69 // read the accumulated time
71  return m_duration;
72 }
73 
74 // read the accumulated time, in seconds
75 double FastTimer::seconds() const {
76  return std::chrono::duration_cast<std::chrono::duration<double>>(m_duration).count();
77 }
78 
79 // if the timer is stopped, read the accumulate time
80 // if the timer is running, also add the time up to "now"
82  return m_duration + ( (m_state == State::kRunning) ? std::chrono::duration_cast<Duration>(Clock::now() - m_start) : Duration::zero() );
83 }
84 
86  return std::chrono::duration_cast<std::chrono::duration<double>>(untilNow()).count();
87 }
88 
89 // return the current state
91  return m_state;
92 }
93 
94 // descrbe the current state
96  static const std::vector<std::string> states{ "stopped", "running", "paused", "unknown" };
97 
98  switch (m_state) {
102  return states[static_cast<unsigned int>(m_state)];
103 
104  default:
105  return states.back();
106  }
107 }
108 
109 FastTimer::Clock::time_point const & FastTimer::getStartTime() const {
110  return m_start;
111 }
112 
113 FastTimer::Clock::time_point const & FastTimer::getStopTime() const {
114  return m_stop;
115 }
116 
117 void FastTimer::setStartTime(FastTimer::Clock::time_point const & time) {
118  if (m_state == State::kStopped) {
119  m_start = time;
120  m_stop = Clock::time_point();
121  m_duration = Duration::zero();
123  } else {
124  std::cerr << "attempting to start a " << describe() << " timer" << std::endl;
125  }
126 }
127 
128 void FastTimer::setStopTime(FastTimer::Clock::time_point const & time) {
129  if (m_state == State::kRunning) {
130  m_stop = time;
131  m_duration += std::chrono::duration_cast<Duration>(m_stop - m_start);
133  } else {
134  std::cerr << "attempting to stop a " << describe() << " timer" << std::endl;
135  }
136 }
Clock::time_point const & getStartTime() const
Definition: FastTimer.cc:109
FastTimer()
Definition: FastTimer.cc:9
void resume()
Definition: FastTimer.cc:51
void setStartTime(Clock::time_point const &)
Definition: FastTimer.cc:117
double secondsUntilNow() const
Definition: FastTimer.cc:85
Duration m_duration
Definition: FastTimer.h:40
Duration untilNow() const
Definition: FastTimer.cc:81
void stop()
Definition: FastTimer.cc:29
void pause()
Definition: FastTimer.cc:40
void setStopTime(Clock::time_point const &)
Definition: FastTimer.cc:128
Clock::time_point m_stop
Definition: FastTimer.h:39
double seconds() const
Definition: FastTimer.cc:75
State state() const
Definition: FastTimer.cc:90
Duration value() const
Definition: FastTimer.cc:70
Clock::time_point const & getStopTime() const
Definition: FastTimer.cc:113
void reset()
Definition: FastTimer.cc:62
Clock::time_point m_start
Definition: FastTimer.h:38
State m_state
Definition: FastTimer.h:41
std::string const & describe() const
Definition: FastTimer.cc:95
std::chrono::nanoseconds Duration
Definition: FastTimer.h:10
void start()
Definition: FastTimer.cc:17