Crocoddyl
 
Loading...
Searching...
No Matches
Stopwatch Class Reference

A class representing a stopwatch. More...

#include <stop-watch.hpp>

Classes

struct  PerformanceData
 Struct to hold the performance data. More...
 
struct  Watcher
 

Public Member Functions

 Stopwatch (StopwatchMode _mode=NONE)
 Constructor.
 
 ~Stopwatch ()
 Destructor.
 
void disable_profiler ()
 Disable the profiler.
 
void enable_profiler ()
 Enable the profiler.
 
long double get_average_time (const std::string &perf_name)
 Returns average execution time of a certain performance.
 
long double get_last_time (const std::string &perf_name)
 Return last measurement of a certain performance.
 
long double get_max_time (const std::string &perf_name)
 Returns maximum execution time of a certain performance.
 
long double get_min_time (const std::string &perf_name)
 Returns minimum execution time of a certain performance.
 
long double get_time_so_far (const std::string &perf_name)
 Return the time since the start of the last measurement of a given performance.
 
long double get_total_time (const std::string &perf_name)
 Returns total execution time of a certain performance.
 
void pause (const std::string &perf_name)
 Stops the stopwatch related to a certain piece of code.
 
bool performance_exists (std::string perf_name)
 Tells if a performance with a certain ID exists.
 
bool profiler_status ()
 Return if the profiler is enable or disable.
 
void report (const std::string &perf_name, int precision=2, std::ostream &output=std::cout)
 Dump the data of a certain performance record.
 
void report_all (int precision=2, std::ostream &output=std::cout)
 Dump the data of all the performance records.
 
void reset (const std::string &perf_name)
 Reset a certain performance record.
 
void reset_all ()
 Resets all the performance records.
 
void set_mode (StopwatchMode mode)
 Initialize stopwatch to use a certain time taking mode.
 
void start (const std::string &perf_name)
 Start the stopwatch related to a certain piece of code.
 
void stop (const std::string &perf_name)
 Stops the stopwatch related to a certain piece of code.
 
long double take_time ()
 Take time, depends on mode.
 
void turn_off ()
 Turn off clock, all the Stopwatch::* methods return without doing anything after this method is called.
 
void turn_on ()
 Turn on clock, restore clock operativity after a turn_off().
 
Watcher watcher (const std::string &perf_name)
 create a Start the stopwatch related to a certain piece of code
 

Protected Member Functions

PerformanceDataget_or_create_perf (const std::string &perf_name)
 
void stop_perf (PerformanceData &perf_info, long double clock_end)
 

Protected Attributes

bool active
 Flag to hold the clock's status.
 
StopwatchMode mode
 Time taking mode.
 
bool profiler_active
 Indicates if the profiler is enabled.
 
std::map< std::string, PerformanceData > * records_of
 Dynamic collection of performance data.
 

Detailed Description

A class representing a stopwatch.

Stopwatch swatch();
A class representing a stopwatch.

The Stopwatch class can be used to measure execution time of code, algorithms, etc., // TODO: he Stopwatch can be initialized in two time-taking modes, CPU time and real time:

swatch.set_mode(REAL_TIME);

CPU time is the time spent by the processor on a certain piece of code, while real time is the real amount of time taken by a certain piece of code to execute (i.e. in general if you are doing hard work such as image or video editing on a different process the measured time will probably increase).

How does it work? Basically, one wraps the code to be measured with the following method calls:

swatch.start("My astounding algorithm");
// Hic est code
swatch.stop("My astounding algorithm");

A string representing the code ID is provided so that nested portions of code can be profiled separately:

swatch.start("My astounding algorithm");
swatch.start("My astounding algorithm - Super smart init");
// Initialization
swatch.stop("My astounding algorithm - Super smart init");
swatch.start("My astounding algorithm - Main loop");
// Loop
swatch.stop("My astounding algorithm - Main loop");
swatch.stop("My astounding algorithm");

Note: ID strings can be whatever you like, in the previous example I have used "My astounding algorithm - *" only to enforce the fact that the measured code portions are part of My astounding algorithm, but there's no connection between the three measurements.

If the code for a certain task is scattered through different files or portions of the same file one can use the start-pause-stop method:

swatch.start("Setup");
// First part of setup
swatch.pause("Setup");
swatch.start("Main logic");
// Main logic
swatch.stop("Main logic");
swatch.start("Setup");
// Cleanup (part of the setup)
swatch.stop("Setup");

Finally, to report the results of the measurements just run:

swatch.report("Code ID");

Thou can also provide an additional std::ostream& parameter to report() to redirect the logging on a different output. Also, you can use the get_total/min/max/average_time() methods to get the individual numeric data, without all the details of the logging. You can also extend Stopwatch to implement your own logging syntax.

To report all the measurements:

swatch.report_all();

Same as above, you can redirect the output by providing a std::ostream& parameter.

Definition at line 154 of file stop-watch.hpp.

Constructor & Destructor Documentation

◆ Stopwatch()

Stopwatch ( StopwatchMode  _mode = NONE)

Constructor.

Definition at line 48 of file stop-watch.cpp.

◆ ~Stopwatch()

~Stopwatch ( )

Destructor.

Definition at line 53 of file stop-watch.cpp.

Member Function Documentation

◆ enable_profiler()

void enable_profiler ( )

Enable the profiler.

Definition at line 55 of file stop-watch.cpp.

◆ disable_profiler()

void disable_profiler ( )

Disable the profiler.

Definition at line 57 of file stop-watch.cpp.

◆ profiler_status()

bool profiler_status ( )
inline

Return if the profiler is enable or disable.

Definition at line 191 of file stop-watch.hpp.

◆ performance_exists()

bool performance_exists ( std::string  perf_name)

Tells if a performance with a certain ID exists.

Definition at line 61 of file stop-watch.cpp.

◆ set_mode()

void set_mode ( StopwatchMode  mode)

Initialize stopwatch to use a certain time taking mode.

Definition at line 59 of file stop-watch.cpp.

◆ watcher()

Stopwatch::Watcher watcher ( const std::string &  perf_name)

create a Start the stopwatch related to a certain piece of code

Definition at line 340 of file stop-watch.cpp.

◆ start()

void start ( const std::string &  perf_name)

Start the stopwatch related to a certain piece of code.

Definition at line 111 of file stop-watch.cpp.

◆ stop()

void stop ( const std::string &  perf_name)

Stops the stopwatch related to a certain piece of code.

Definition at line 126 of file stop-watch.cpp.

◆ pause()

void pause ( const std::string &  perf_name)

Stops the stopwatch related to a certain piece of code.

Definition at line 162 of file stop-watch.cpp.

◆ reset()

void reset ( const std::string &  perf_name)

Reset a certain performance record.

Definition at line 218 of file stop-watch.cpp.

◆ reset_all()

void reset_all ( )

Resets all the performance records.

Definition at line 183 of file stop-watch.cpp.

◆ report()

void report ( const std::string &  perf_name,
int  precision = 2,
std::ostream &  output = std::cout 
)

Dump the data of a certain performance record.

Definition at line 246 of file stop-watch.cpp.

◆ report_all()

void report_all ( int  precision = 2,
std::ostream &  output = std::cout 
)

Dump the data of all the performance records.

Definition at line 193 of file stop-watch.cpp.

◆ get_total_time()

long double get_total_time ( const std::string &  perf_name)

Returns total execution time of a certain performance.

Definition at line 290 of file stop-watch.cpp.

◆ get_average_time()

long double get_average_time ( const std::string &  perf_name)

Returns average execution time of a certain performance.

Definition at line 300 of file stop-watch.cpp.

◆ get_min_time()

long double get_min_time ( const std::string &  perf_name)

Returns minimum execution time of a certain performance.

Definition at line 310 of file stop-watch.cpp.

◆ get_max_time()

long double get_max_time ( const std::string &  perf_name)

Returns maximum execution time of a certain performance.

Definition at line 320 of file stop-watch.cpp.

◆ get_last_time()

long double get_last_time ( const std::string &  perf_name)

Return last measurement of a certain performance.

Definition at line 330 of file stop-watch.cpp.

◆ get_time_so_far()

long double get_time_so_far ( const std::string &  perf_name)

Return the time since the start of the last measurement of a given performance.

Definition at line 277 of file stop-watch.cpp.

◆ turn_off()

void turn_off ( )

Turn off clock, all the Stopwatch::* methods return without doing anything after this method is called.

Definition at line 241 of file stop-watch.cpp.

◆ turn_on()

void turn_on ( )

Turn on clock, restore clock operativity after a turn_off().

Definition at line 236 of file stop-watch.cpp.

◆ take_time()

long double take_time ( )

Take time, depends on mode.

Definition at line 65 of file stop-watch.cpp.

◆ get_or_create_perf()

Stopwatch::PerformanceData & get_or_create_perf ( const std::string &  perf_name)
protected

Definition at line 105 of file stop-watch.cpp.

◆ stop_perf()

void stop_perf ( PerformanceData perf_info,
long double  clock_end 
)
protected

Definition at line 140 of file stop-watch.cpp.

Member Data Documentation

◆ active

bool active
protected

Flag to hold the clock's status.

Definition at line 280 of file stop-watch.hpp.

◆ mode

StopwatchMode mode
protected

Time taking mode.

Definition at line 281 of file stop-watch.hpp.

◆ records_of

std::map<std::string, PerformanceData>* records_of
protected

Dynamic collection of performance data.

Definition at line 283 of file stop-watch.hpp.

◆ profiler_active

bool profiler_active
protected

Indicates if the profiler is enabled.

Definition at line 284 of file stop-watch.hpp.


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