Skip to content

Timer

A simple, cross-platform timer class based on <chrono>.

Header: #include <asw/util/Timer.h>

Class: Timer

cpp
class Timer;

Methods

start

cpp
void start();

Start the timer.

stop

cpp
void stop();

Stop the timer. The elapsed time is preserved.

isRunning

cpp
bool isRunning() const;

Returns true if the timer is currently running.

reset

cpp
void reset();

Reset the timer to zero.

getElapsedTime

cpp
template <typename Precision>
double getElapsedTime();

Get the elapsed time in the given precision. If the timer is running, it returns the time up to now. If stopped, it returns the time between start and stop.

PrecisionUnit
std::chrono::secondsSeconds
std::chrono::millisecondsMilliseconds
std::chrono::microsecondsMicroseconds
std::chrono::nanosecondsNanoseconds

Example

cpp
#include <asw/util/Timer.h>

Timer timer;
timer.start();

// ... do work ...

double ms = timer.getElapsedTime<std::chrono::milliseconds>();
double sec = timer.getElapsedTime<std::chrono::seconds>();

timer.stop();
timer.reset();

Released under the MIT License.