Medusa  1.1
Coordinate Free Mehless Method implementation
Stopwatch.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_UTILS_STOPWATCH_HPP_
2 #define MEDUSA_BITS_UTILS_STOPWATCH_HPP_
3 
11 #include <medusa/Config.hpp>
12 #include <vector>
13 #include <chrono>
14 #include <string>
15 #include <ostream>
16 
17 namespace mm {
18 
31 class Stopwatch {
32  typedef std::chrono::steady_clock::time_point time_type;
33  std::vector<std::string> labels;
34  std::vector<double> cumulative_time;
35  std::vector<int> counts;
36  std::vector<time_type> times;
37  std::vector<bool> currently_running;
38 
40  int getID(const std::string& label) const;
41 
42  public:
44  void start(const std::string& label);
50  void stop(const std::string& label);
52  double cumulativeTime(const std::string& label) const;
54  int numLaps(const std::string& label) const;
56  double timePerLap(const std::string& label) const;
58  bool isRunning(const std::string& label) const { return currently_running[getID(label)]; }
60  void clear();
62  friend std::ostream& operator<<(std::ostream& os, const Stopwatch& stopwatch);
63 };
64 
65 } // namespace mm
66 
67 #endif // MEDUSA_BITS_UTILS_STOPWATCH_HPP_
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::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
mm::Stopwatch::time_type
std::chrono::steady_clock::time_point time_type
Time type.
Definition: Stopwatch.hpp:32
Config.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
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::operator<<
friend std::ostream & operator<<(std::ostream &os, const Stopwatch &stopwatch)
Output average lap times for all labels.
Definition: Stopwatch.cpp:76
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