Stride Reference Manual  - generated for commit 9643b11
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Stopwatch.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #pragma once
22 
23 #include "TimeToString.h"
24 #include <chrono>
25 #include <string>
26 
27 namespace stride {
28 namespace util {
29 
34 template <typename T = std::chrono::steady_clock>
35 class Stopwatch
36 {
37 public:
38  using TClock = T;
39 
41  explicit Stopwatch(std::string name = "stopwatch", bool running = false)
42  : m_accumulated(T::duration::zero()), m_last_start(), m_name(std::move(name)), m_running(running)
43  {
44  if (m_running) {
45  m_last_start = T::now();
46  }
47  }
48 
51  {
52  if (!m_running) {
53  m_running = true;
54  m_last_start = T::now();
55  }
56  return *this;
57  }
58 
61  {
62  if (m_running) {
63  m_accumulated += (T::now() - m_last_start);
64  m_running = false;
65  }
66  return *this;
67  }
68 
71  {
72  m_accumulated = T::duration::zero();
73  m_running = false;
74  return *this;
75  }
76 
78  bool IsRunning() const { return (m_running); }
79 
81  std::string GetName() const { return m_name; }
82 
84  typename T::duration Get() const
85  {
86  auto fTemp = m_accumulated;
87  if (m_running) {
88  fTemp += (T::now() - m_last_start);
89  }
90  return fTemp;
91  }
92 
94  std::string ToString() const
95  {
96  using namespace std;
97  using namespace std::chrono;
98 
99  string colon_string;
100  typedef typename TClock::period TPeriod;
101  if (ratio_less_equal<TPeriod, micro>::value) {
102  microseconds d = duration_cast<microseconds>(Get());
103  colon_string = TimeToString::ToColonString(d);
104  } else if (ratio_less_equal<TPeriod, milli>::value) {
105  milliseconds d = duration_cast<milliseconds>(Get());
106  colon_string = TimeToString::ToColonString(d);
107  } else {
108  seconds d = duration_cast<seconds>(Get());
109  colon_string = TimeToString::ToColonString(d);
110  }
111  return colon_string;
112  }
113 
114 private:
115  typename T::duration m_accumulated;
116  typename T::time_point m_last_start;
117  std::string m_name;
118  bool m_running;
119 };
120 
124 template <typename T>
125 std::ostream& operator<<(std::ostream& oss, Stopwatch<T> const& stopwatch)
126 {
127  return (oss << stopwatch.ToString());
128 }
129 
130 } // namespace util
131 } // namespace stride
T::time_point m_last_start
Definition: Stopwatch.h:116
Utilities to tag clocks and reformat clock readout to string.
Stopwatch & Start()
Starts stopwatch if it was stopped.
Definition: Stopwatch.h:50
std::string GetName() const
Return name of this stopwatch.
Definition: Stopwatch.h:81
Stopwatch & Stop()
Stops the stopwatch if it was running.
Definition: Stopwatch.h:60
Provides a stopwatch interface to time: it accumulates time between start/stop pairs.
Definition: Stopwatch.h:35
bool IsRunning() const
Reports whether stopwatch has been started.
Definition: Stopwatch.h:78
std::string ToString() const
Returns string representation of readout.
Definition: Stopwatch.h:94
STL namespace.
static std::string ToColonString(std::chrono::minutes d)
Produce string in hh:mm:ss format.
Stopwatch & Reset()
Resets stopwatch i.e. stopwatch is stopped and time accumulator is cleared.
Definition: Stopwatch.h:70
T::duration m_accumulated
Definition: Stopwatch.h:115
Stopwatch(std::string name="stopwatch", bool running=false)
Constructor initializes stopwatch.
Definition: Stopwatch.h:41
T::duration Get() const
Returns the accumulated value without altering the stopwatch state.
Definition: Stopwatch.h:84
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28