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.
 
void reset ()
 Resets the timer.
 
DateTime startingTime () const
 Returns the date/time at starting the timer.
 
Duration stop ()
 Stops the timer and returns the measured duration.
 
Duration stopAndReset ()
 Stops and resets the timer.
 
DateTime stoppingTime () const
 Returns the date/time at stopping the timer.
 
 StopWatch (ClockType clockType=ClockType::Virtual)
 Constructs a StopWatch and starts it immediately.
 
virtual ~StopWatch ()
 Destructs the StopWatch.
 

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.
 

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 << ".";
Represents a duration.
Definition Duration.h:17
Duration stop()
Stops the timer and returns the measured duration.
Definition StopWatch.cpp:39
StopWatch(ClockType clockType=ClockType::Virtual)
Constructs a StopWatch and starts it immediately.
Definition StopWatch.cpp:19
// By executing a lambda.
Duration duration = StopWatch::measure([&]() {
long_operation();
});
std::cout << "Operation took " << duration << ".";
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:31

Constructor & Destructor Documentation

◆ StopWatch()

Constructs a StopWatch and starts it immediately.

Parameters
clockTypeClock type.

◆ ~StopWatch()

~StopWatch ( )
virtual

Destructs the StopWatch.

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.

◆ 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.

◆ reset()

void reset ( )

Resets the timer.

◆ startingTime()

DateTime startingTime ( ) const

Returns the date/time at starting the timer.

Returns
Date/time at starting the timer.

◆ stop()

Duration stop ( )

Stops the timer and returns the measured duration.

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

◆ 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.

◆ 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.

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