Medusa  1.1
Coordinate Free Mehless Method implementation
Stopwatch.cpp
Go to the documentation of this file.
1 #include <algorithm>
3 #include <iomanip>
5 
11 namespace mm {
12 
13 int Stopwatch::getID(const std::string& label) const {
14  auto it = std::find(labels.begin(), labels.end(), label);
15  assert_msg(it != labels.end(), "Label '%s' not found. Available labels: %s.",
16  label.c_str(), labels);
17  return it - labels.begin();
18 }
19 void Stopwatch::start(const std::string& label) {
20  for (int i = 0; i < static_cast<int>(labels.size()); i++) {
21  if (labels[i] == label) {
23  "start() for label '%s' was called more than once"
24  " before stop() call.", label.c_str());
25  times[i] = std::chrono::steady_clock::now();
26  currently_running[i] = true;
27  return;
28  }
29  }
30  // label not found
31  labels.push_back(label);
32  times.push_back(std::chrono::steady_clock::now());
33  cumulative_time.push_back(0.0);
34  counts.push_back(0);
35  currently_running.push_back(true);
36 }
37 void Stopwatch::stop(const std::string& label) {
38  int id = getID(label);
40  "stop() for label '%s' was called"
41  " more than once before start() call.",
42  label.c_str());
43  ++counts[id];
44  cumulative_time[id] += std::chrono::duration<double>(
45  std::chrono::steady_clock::now()
46  - times[getID(label)]).count();
47  currently_running[id] = false;
48 }
49 double Stopwatch::cumulativeTime(const std::string& label) const {
50  assert_msg(!isRunning(label),
51  "Stopwatch with label '%s' is still running, `stop()` must be called"
52  " before results can be displayed.", label.c_str());
53  return cumulative_time[getID(label)];
54 }
55 int Stopwatch::numLaps(const std::string& label) const {
56  assert_msg(!isRunning(label),
57  "Stopwatch with label '%s' is still running, `stop()` must be called"
58  " before results can be displayed.", label.c_str());
59  return counts[getID(label)];
60 }
61 double Stopwatch::timePerLap(const std::string& label) const {
62  assert_msg(!isRunning(label),
63  "Stopwatch with label '%s' is still running, `stop()` must be called"
64  " before results can be displayed.", label.c_str());
65  return cumulativeTime(label)/static_cast<double>(numLaps(label));
66 }
68  times.clear();
69  labels.clear();
70  counts.clear();
71  cumulative_time.clear();
72  currently_running.clear();
73 }
74 
76 std::ostream& operator<<(std::ostream& os, const Stopwatch& stopwatch) {
77  size_t maxs = 0;
78  for (const auto& label : stopwatch.labels) {
79  if (label.size() > maxs) {
80  maxs = label.size();
81  }
82  }
83  for (const auto& label : stopwatch.labels) {
84  os << std::setw(maxs) << label << ": " << stopwatch.timePerLap(label) << " [s]\n";
85  }
86  return os;
87 }
88 
89 } // namespace mm
mm::Stopwatch::labels
std::vector< std::string > labels
List of stopwatch labels.
Definition: Stopwatch.hpp:33
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
mm::Stopwatch::numLaps
int numLaps(const std::string &label) const
Returns number of laps for a given label.
Definition: Stopwatch.cpp:55
mm::Stopwatch::clear
void clear()
Clear all stopwatch related data.
Definition: Stopwatch.cpp:67
mm::Stopwatch::cumulative_time
std::vector< double > cumulative_time
List of cumulative times for each stopwatch.
Definition: Stopwatch.hpp:34
mm::operator<<
std::ostream & operator<<(std::ostream &os, const Gaussian< S > &b)
Output basic information about given Gaussian RBF.
Definition: Gaussian.hpp:37
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
mm::Stopwatch::cumulativeTime
double cumulativeTime(const std::string &label) const
Returns total time of all laps for a given label.
Definition: Stopwatch.cpp:49
mm::Stopwatch
A simple stopwatch class: time sections of code that execute repeatedly and get average execution tim...
Definition: Stopwatch.hpp:31
mm::Stopwatch::timePerLap
double timePerLap(const std::string &label) const
Returns average time spent per lap.
Definition: Stopwatch.cpp:61
Stopwatch.hpp
mm::Stopwatch::currently_running
std::vector< bool > currently_running
For tracking when stopwatch is running.
Definition: Stopwatch.hpp:37
mm::Stopwatch::start
void start(const std::string &label)
Starts the stopwatch with a given label.
Definition: Stopwatch.cpp:19
assert.hpp
mm::Stopwatch::times
std::vector< time_type > times
List of times for each stopwatch.
Definition: Stopwatch.hpp:36
mm::Stopwatch::isRunning
bool isRunning(const std::string &label) const
Returns if stopwatch with a given label is currently running.
Definition: Stopwatch.hpp:58
mm::Stopwatch::counts
std::vector< int > counts
List of lap counts for each stopwatch.
Definition: Stopwatch.hpp:35
mm::Stopwatch::stop
void stop(const std::string &label)
Stops the stopwatch with a given label.
Definition: Stopwatch.cpp:37
mm::Stopwatch::getID
int getID(const std::string &label) const
Returns the ID of stopwatch with a given label.
Definition: Stopwatch.cpp:13