Medusa  1.1
Coordinate Free Mehless Method implementation
mm::Stopwatch Class Reference

#include <Stopwatch.hpp>

Detailed Description

A simple stopwatch class: time sections of code that execute repeatedly and get average execution time.

Stopwatch keeps track of cumulative time spent between all calls to start(label), stop(label) pairs for the same label, number of laps (calls of start/stop pairs), and cumulative time per lap.

For timing execution time between various checkpoints, use the Timer class.

Stopwatch s;
for (int i = 0; i < 10; ++i) {
// ... code: NOT timed ...
s.start("section1");
// ... code: timed ...
s.stop("section1");
// ... code: NOT timed ...
s.start("section2");
// ... code: timed ...
s.stop("section2");
}
s.cumulativeTime("section1"); // Returns cumulative time.
s.numLaps("section1"); // Returns number of laps counted.
s.timePerLap("section1"); // Returns average time per lap.
std::cout << s << std::endl;
See also
Timer

Definition at line 31 of file Stopwatch.hpp.

Public Member Functions

void start (const std::string &label)
 Starts the stopwatch with a given label. More...
 
void stop (const std::string &label)
 Stops the stopwatch with a given label. More...
 
double cumulativeTime (const std::string &label) const
 Returns total time of all laps for a given label. More...
 
int numLaps (const std::string &label) const
 Returns number of laps for a given label. More...
 
double timePerLap (const std::string &label) const
 Returns average time spent per lap. More...
 
bool isRunning (const std::string &label) const
 Returns if stopwatch with a given label is currently running. More...
 
void clear ()
 Clear all stopwatch related data. More...
 

Friends

std::ostream & operator<< (std::ostream &os, const Stopwatch &stopwatch)
 Output average lap times for all labels. More...
 

Private Types

typedef std::chrono::steady_clock::time_point time_type
 Time type. More...
 

Private Member Functions

int getID (const std::string &label) const
 Returns the ID of stopwatch with a given label. More...
 

Private Attributes

std::vector< std::string > labels
 List of stopwatch labels. More...
 
std::vector< double > cumulative_time
 List of cumulative times for each stopwatch. More...
 
std::vector< int > counts
 List of lap counts for each stopwatch. More...
 
std::vector< time_typetimes
 List of times for each stopwatch. More...
 
std::vector< bool > currently_running
 For tracking when stopwatch is running. More...
 

Member Function Documentation

◆ clear()

void mm::Stopwatch::clear ( )

Clear all stopwatch related data.

Definition at line 67 of file Stopwatch.cpp.

◆ cumulativeTime()

double mm::Stopwatch::cumulativeTime ( const std::string &  label) const

Returns total time of all laps for a given label.

Definition at line 49 of file Stopwatch.cpp.

◆ getID()

int mm::Stopwatch::getID ( const std::string &  label) const
private

Returns the ID of stopwatch with a given label.

Definition at line 13 of file Stopwatch.cpp.

◆ isRunning()

bool mm::Stopwatch::isRunning ( const std::string &  label) const
inline

Returns if stopwatch with a given label is currently running.

Definition at line 58 of file Stopwatch.hpp.

◆ numLaps()

int mm::Stopwatch::numLaps ( const std::string &  label) const

Returns number of laps for a given label.

Definition at line 55 of file Stopwatch.cpp.

◆ start()

void mm::Stopwatch::start ( const std::string &  label)

Starts the stopwatch with a given label.

Definition at line 19 of file Stopwatch.cpp.

◆ stop()

void mm::Stopwatch::stop ( const std::string &  label)

Stops the stopwatch with a given label.

Updates cumulative time and number of laps information for the given label. Individual lap durations are not saved.

Definition at line 37 of file Stopwatch.cpp.

◆ timePerLap()

double mm::Stopwatch::timePerLap ( const std::string &  label) const

Returns average time spent per lap.

Definition at line 61 of file Stopwatch.cpp.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Stopwatch stopwatch 
)
friend

Output average lap times for all labels.

Definition at line 76 of file Stopwatch.cpp.

Member Data Documentation

◆ counts

std::vector<int> mm::Stopwatch::counts
private

List of lap counts for each stopwatch.

Definition at line 35 of file Stopwatch.hpp.

◆ cumulative_time

std::vector<double> mm::Stopwatch::cumulative_time
private

List of cumulative times for each stopwatch.

Definition at line 34 of file Stopwatch.hpp.

◆ currently_running

std::vector<bool> mm::Stopwatch::currently_running
private

For tracking when stopwatch is running.

Definition at line 37 of file Stopwatch.hpp.

◆ labels

std::vector<std::string> mm::Stopwatch::labels
private

List of stopwatch labels.

Definition at line 33 of file Stopwatch.hpp.

◆ times

std::vector<time_type> mm::Stopwatch::times
private

List of times for each stopwatch.

Definition at line 36 of file Stopwatch.hpp.


The documentation for this class was generated from the following files: