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

#include <Timer.hpp>

Detailed Description

Simple timer class: add checkpoints throughout the code and measure execution time between them.

When timing parts of code for example inside loops Stopwatch should be used instead.

Usage example:

Timer timer;
timer.addCheckPoint("beg");
//... code ...
timer.addCheckPoint("mid");
// ...more code ...
timer.addCheckPoint("end");
timer.showTimings("beg", "mid"); // shows time between two checkpoints
std::cout << timer << std::endl;
double t = timer.duration("beg", "end"); // time difference in seconds
See also
Stopwatch

Definition at line 29 of file Timer.hpp.

Public Member Functions

void addCheckPoint (const std::string &label)
 Adds a checkpoint with given label and remembers the time at which it was added. More...
 
void showTimings (std::ostream &os=std::cout) const
 Pretty print all durations between checkpoints. More...
 
void showTimings (const std::string &from, const std::string &to, std::ostream &os=std::cout) const
 Output duration between the checkpoints with given labels to os. More...
 
time_type timeAt (const std::string &label) const
 Return absolute time for a given label. More...
 
double duration (const std::string &from, const std::string &to) const
 Return time difference in seconds between two checkpoints. More...
 
double durationToNow (const std::string &from) const
 Return time difference in seconds between from and now. More...
 
int size () const
 Return the number of measurements taken. More...
 
const std::vector< std::string > & labels () const
 Return all labels. More...
 
const std::vector< time_type > & times () const
 Return all times. More...
 
void clear ()
 Clear all of time points. More...
 

Friends

std::ostream & operator<< (std::ostream &os, const Timer &timer)
 Output all times between all checkpoints. More...
 

Protected Types

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

Protected Member Functions

void showTimings (int from, int to, std::ostream &os=std::cout) const
 Output timings between the checkpoints with given ids to os. More...
 
int getID (const std::string &label) const
 Returns the ID of checkpoints with a given label. More...
 
time_type timeAt (int id) const
 Return absolute time for a given id. More...
 

Protected Attributes

std::vector< std::string > labels_
 List of checkpoint labels. More...
 
std::vector< time_typetimes_
 List of checkpoint times. More...
 

Member Function Documentation

◆ addCheckPoint()

void mm::Timer::addCheckPoint ( const std::string &  label)

Adds a checkpoint with given label and remembers the time at which it was added.

Definition at line 12 of file Timer.cpp.

◆ clear()

void mm::Timer::clear ( )

Clear all of time points.

Definition at line 73 of file Timer.cpp.

◆ duration()

double mm::Timer::duration ( const std::string &  from,
const std::string &  to 
) const

Return time difference in seconds between two checkpoints.

Definition at line 60 of file Timer.cpp.

◆ durationToNow()

double mm::Timer::durationToNow ( const std::string &  from) const

Return time difference in seconds between from and now.

Definition at line 63 of file Timer.cpp.

◆ getID()

int mm::Timer::getID ( const std::string &  label) const
protected

Returns the ID of checkpoints with a given label.

Definition at line 67 of file Timer.cpp.

◆ labels()

const std::vector<std::string>& mm::Timer::labels ( ) const
inline

Return all labels.

Definition at line 65 of file Timer.hpp.

◆ showTimings() [1/3]

void mm::Timer::showTimings ( const std::string &  from,
const std::string &  to,
std::ostream &  os = std::cout 
) const

Output duration between the checkpoints with given labels to os.

Example: showTimings("begin", "end");

Definition at line 38 of file Timer.cpp.

◆ showTimings() [2/3]

void mm::Timer::showTimings ( int  from,
int  to,
std::ostream &  os = std::cout 
) const
protected

Output timings between the checkpoints with given ids to os.

Definition at line 43 of file Timer.cpp.

◆ showTimings() [3/3]

void mm::Timer::showTimings ( std::ostream &  os = std::cout) const

Pretty print all durations between checkpoints.

If there are less than 2 checkpoints, only a warning is printed.

Definition at line 18 of file Timer.cpp.

◆ size()

int mm::Timer::size ( ) const
inline

Return the number of measurements taken.

Definition at line 63 of file Timer.hpp.

◆ timeAt() [1/2]

Timer::time_type mm::Timer::timeAt ( const std::string &  label) const

Return absolute time for a given label.

Definition at line 57 of file Timer.cpp.

◆ timeAt() [2/2]

Timer::time_type mm::Timer::timeAt ( int  id) const
protected

Return absolute time for a given id.

Definition at line 52 of file Timer.cpp.

◆ times()

const std::vector<time_type>& mm::Timer::times ( ) const
inline

Return all times.

Definition at line 67 of file Timer.hpp.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Timer timer 
)
friend

Output all times between all checkpoints.

Definition at line 72 of file Timer.hpp.

Member Data Documentation

◆ labels_

std::vector<std::string> mm::Timer::labels_
protected

List of checkpoint labels.

Definition at line 32 of file Timer.hpp.

◆ times_

std::vector<time_type> mm::Timer::times_
protected

List of checkpoint times.

Definition at line 33 of file Timer.hpp.


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