StopWatch Class Reference

Measures the passed time between the construction or calling reset() and stop(). More...

#include <ArmarXCore/core/time/StopWatch.h>

+ Inheritance diagram for StopWatch:

Public Member Functions

bool isStopped () const
 Returns whether the timer is stopped or is actively measuring time. More...
 
void reset ()
 Resets the timer. More...
 
DateTime startingTime () const
 Returns the date/time at starting the timer. More...
 
Duration stop ()
 Stops the timer and returns the measured duration. More...
 
Duration stopAndReset ()
 Stops and resets the timer. More...
 
DateTime stoppingTime () const
 Returns the date/time at stopping the timer. More...
 
 StopWatch (ClockType clockType=ClockType::Virtual)
 Constructs a StopWatch and starts it immediately. More...
 
virtual ~StopWatch ()
 Destructs the StopWatch. More...
 

Static Public Member Functions

static Duration measure (std::function< void(void)> subjectToMeasure, ClockType clockType=ClockType::Virtual)
 Measures the duration needed to execute the given lambda and returns it. More...
 

Detailed Description

Measures the passed time between the construction or calling reset() and stop().

The StopWatch uses the system time by default, but it may use the virtual time provided by the time server as well. Also has a static method measure(), which takes a lambda, and returns the time it took to execute that lambda.

Code examples:

// By supplied methods.
long_operation();
Duration duration = sw.stop();
std::cout << "Operation took " << duration << ".";
// By executing a lambda.
Duration duration = StopWatch::measure([&]() {
long_operation();
});
std::cout << "Operation took " << duration << ".";

Definition at line 42 of file StopWatch.h.

Constructor & Destructor Documentation

◆ StopWatch()

Constructs a StopWatch and starts it immediately.

Parameters
clockTypeClock type.

Definition at line 20 of file StopWatch.cpp.

◆ ~StopWatch()

~StopWatch ( )
virtual

Destructs the StopWatch.

Definition at line 27 of file StopWatch.cpp.

Member Function Documentation

◆ isStopped()

bool isStopped ( ) const

Returns whether the timer is stopped or is actively measuring time.

Returns
True of timer is stopped, false otherwise.

Definition at line 59 of file StopWatch.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ measure()

Duration measure ( std::function< void(void)>  subjectToMeasure,
ClockType  clockType = ClockType::Virtual 
)
static

Measures the duration needed to execute the given lambda and returns it.

Parameters
subjectToMeasureLambda to be measured
clockTypeClock type.
Returns
Time it took to execute the given lambda.

Definition at line 34 of file StopWatch.cpp.

◆ reset()

void reset ( )

Resets the timer.

Definition at line 51 of file StopWatch.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ startingTime()

DateTime startingTime ( ) const

Returns the date/time at starting the timer.

Returns
Date/time at starting the timer.

Definition at line 66 of file StopWatch.cpp.

◆ stop()

Duration stop ( )

Stops the timer and returns the measured duration.

Returns
Duration elapsed since construction or last call of reset().

Definition at line 43 of file StopWatch.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ stopAndReset()

Duration stopAndReset ( )

Stops and resets the timer.

It returns the date/time at stopping the timer.

Returns
Date/time at stopping the timer.
Exceptions
std::logic_errorWhen the timer was not stopped yet.

Definition at line 84 of file StopWatch.cpp.

+ Here is the call graph for this function:

◆ stoppingTime()

DateTime stoppingTime ( ) const

Returns the date/time at stopping the timer.

Returns
Date/time at stopping the timer.
Exceptions
std::logic_errorWhen the timer was not stopped yet.

Definition at line 73 of file StopWatch.cpp.

+ Here is the call graph for this function:

The documentation for this class was generated from the following files:
armarx::core::time::StopWatch::measure
static Duration measure(std::function< void(void)> subjectToMeasure, ClockType clockType=ClockType::Virtual)
Measures the duration needed to execute the given lambda and returns it.
Definition: StopWatch.cpp:34
armarx::core::time::StopWatch::StopWatch
StopWatch(ClockType clockType=ClockType::Virtual)
Constructs a StopWatch and starts it immediately.
Definition: StopWatch.cpp:20
armarx::armem::Duration
armarx::core::time::Duration Duration
Definition: forward_declarations.h:14