Class timer_t

Class Documentation

class timer_t

Utility timer for elapsed-time measurement and pacing.

The timer measures elapsed time between tic() and toc() and provides sleep/pacing helpers for loop-rate control.

Public Types

using unique_ptr = std::unique_ptr<timer_t>

Smart pointers &#8212; unique.

using shared_ptr = std::shared_ptr<timer_t>

Smart pointers &#8212; shared.

Public Functions

inline timer_t()

Default constructor. This function starts the timer.

~timer_t() = default

Default destructor.

inline void tic()

Set/Reset the timer for measuring elapsed time.

inline duration_t toc()

Stops the timer and returns the elapsed time.

Returns:

Elapsed duration since the last tic().

inline bool over(duration_t minDuration_)

Checks if the timer is running for more than the specified duration

Parameters:

minDuration_ – Duration threshold used for the comparison.

Returns:

true when elapsed time is greater than minDuration_.

inline duration_t pace(duration_t minDuration_)

Either do nothing or sleep to pace the execution to a minimum FPS.

Parameters:

minDuration_ – Minimum cycle duration to enforce.

Returns:

Elapsed duration since the last tic() after pacing.

inline void sleep(duration_t duration_)

Waits for a specified duration.

Parameters:

duration_ – Sleep duration requested for the current thread.

inline void print(std::string prefix_ = "")

Prints elapsed time since the last tic().

Parameters:

prefix_ – Prefix prepended to the printed timer message.

Public Static Functions

static inline timer_t::unique_ptr make_unique()

Make unique pointer function

Returns:

Unique pointer owning a new timer instance.

static inline timer_t::shared_ptr make_shared()

Make shared pointer function

Returns:

Shared pointer owning a new timer instance.

Private Functions

inline duration_t __elapsed_time(duration_t offset_)

Computes the elapsed time since the last tic() call (0 if negative)

Parameters:

offset_ – Target duration used to compute remaining wait time.

Returns:

Remaining duration until offset_, clamped to zero.

inline duration_t __toc()

Computes the elapsed time since the last tic() call

Returns:

Elapsed duration in microseconds since tic().

inline void __sleep(duration_t duration_)

Sleeps a thread for a specified duration

Parameters:

duration_ – Duration used in std::this_thread::sleep_for.

Private Members

time_point<high_resolution_clock> startTime

Start time.