ROSE  0.11.96.11
Public Types | Public Member Functions | Static Public Member Functions | List of all members
Sawyer::Stopwatch Class Reference

Description

Simple elapsed time.

Stopwatch stopwatch; // starts immediately unless false argument is given
do_something();
std::cerr <<"that took " <<stopwatch <<" seconds.\n";

All times are returned as floating point number of seconds. The underlying data structure has nanosecond resolution although accuracy is probably nowhere near that.

A stopwatch can be started and stopped as often as desired and it will accumulate the total amount of time it has been in the running state (the clear() method resets the stopwatch by stopping it and zeroing the total). Starting an already running stopwatch does nothing, and neither does stopping an already stopped stopwatch.

When a stopwatch is copied the destination stopwatch inherits the accumulated time and running state of the source. Both instances are then completely independent of one another.

Thread safety: These functions are not thread-safe, although multiple threads can invoke the methods concurrently on different objects. It is permissible for different threads to invoke methods on the same object provided that synchronization occurs above the method calls.

Definition at line 41 of file Stopwatch.h.

#include <Stopwatch.h>

Public Types

typedef double TimePoint
 
typedef double Duration
 

Public Member Functions

 Stopwatch (bool start=true)
 Construct and optionally start a timer. More...
 
double clear (double value=0.0)
 Stop and reset the timer to the specified value. More...
 
double restart ()
 Restart the timer. More...
 
double stop (bool clear=false)
 Stop the timer and report accumulated time. More...
 
double report (bool clear=false) const
 Report current accumulated time without stopping or starting. More...
 
bool isRunning () const
 Query state of stopwatch. More...
 
std::string toString () const
 Convert a stopwatch to a human-readalbe string.
 
double start ()
 Start the timer and report accumulated time. More...
 
double start (double value)
 Start the timer and report accumulated time. More...
 

Static Public Member Functions

static std::string toString (double seconds)
 Format time in human readable manner.
 

Constructor & Destructor Documentation

◆ Stopwatch()

Sawyer::Stopwatch::Stopwatch ( bool  start = true)
inlineexplicit

Construct and optionally start a timer.

The timer is started immediately unless the constructor is invoked with a false argument.

Definition at line 62 of file Stopwatch.h.

Member Function Documentation

◆ clear()

double Sawyer::Stopwatch::clear ( double  value = 0.0)

Stop and reset the timer to the specified value.

The timer is stopped if it is running. Returns the accumulated time before resetting it to the specified value.

◆ start() [1/2]

double Sawyer::Stopwatch::start ( )

Start the timer and report accumulated time.

Reports the time accumulated as of this call and makes sure the clock is running. If a value is specified then the stopwatch starts with that much time accumulated.

◆ start() [2/2]

double Sawyer::Stopwatch::start ( double  value)

Start the timer and report accumulated time.

Reports the time accumulated as of this call and makes sure the clock is running. If a value is specified then the stopwatch starts with that much time accumulated.

◆ restart()

double Sawyer::Stopwatch::restart ( )

Restart the timer.

Reports the accumulated time as of this call, then resets it to zero and starts or restarts the clock running.

◆ stop()

double Sawyer::Stopwatch::stop ( bool  clear = false)

Stop the timer and report accumulated time.

If the timer is running then it is stopped and the elapsed time is accumulated, otherwise the timer remains in the stopped state and no additional time is accumulated. If clear is set, then the accumlated time is reset to zero. In any case, the return value is the accumulated time before being optionally reset to zero.

◆ report()

double Sawyer::Stopwatch::report ( bool  clear = false) const

Report current accumulated time without stopping or starting.

If clear is set then the accumulated time is reset to zero without affecting the return value or the running state, and the stopwatch starts reaccumulating time as of this call.

◆ isRunning()

bool Sawyer::Stopwatch::isRunning ( ) const
inline

Query state of stopwatch.

Returns true if and only if the stopwatch is running.

Definition at line 102 of file Stopwatch.h.


The documentation for this class was generated from the following file:
Sawyer::Stopwatch::Stopwatch
Stopwatch(bool start=true)
Construct and optionally start a timer.
Definition: Stopwatch.h:62