cli11
Loading...
Searching...
No Matches
cli::timer_t Class Referenceexport
module cli11

Measures elapsed wall-clock time from construction. More...

Inheritance diagram for cli::timer_t:
cli::auto_timer_t

Public Member Functions

 timer_t (std::string title="Timer", time_print_t time_print=simple)
 Starts a timer.
template<std::invocable F>
auto time_it (F &&f, double target_time=1.0) -> std::string
 Repeatedly invokes f until target_time has elapsed.
auto make_time_str () const -> std::string
 Formats the time elapsed since construction, divided by the cycle count.
auto make_time_str (double time) const -> std::string
 Formats a duration, choosing a unit that keeps the value readable.
auto to_string () const -> std::string
 Renders the measurement through the configured print callable.
auto operator/ (std::size_t val) -> timer_t &
 Sets the divisor applied to the measured interval.

Static Public Member Functions

static auto simple (std::string title, std::string time) -> std::string
 Renders a measurement as "title: time".
static auto big (std::string title, std::string time) -> std::string
 Renders a measurement inside a full-width banner.

Protected Types

using clock_t = std::chrono::steady_clock
 The clock used for all measurements.
using time_point_t = std::chrono::time_point<clock_t>
 A point in time as reported by clock_t.
using time_print_t = std::function<std::string(std::string, std::string)>
 Renders a finished measurement given a title and a formatted duration.

Protected Attributes

std::string title_
 Label reported alongside the measurement.
time_print_t time_print_
 Callable used to render the finished measurement.
time_point_t start_
 The instant this timer started measuring.
std::size_t cycles_ {1}
 Number of cycles the measured interval is divided by.

Detailed Description

Measures elapsed wall-clock time from construction.

The timer starts when it is constructed. to_string renders the time elapsed since then, divided by the cycle count set through operator/.

Member Typedef Documentation

◆ clock_t

using cli::timer_t::clock_t = std::chrono::steady_clock
protected

The clock used for all measurements.

Note
This member alias shadows std::clock_t inside the class body. Nothing here refers to the C library type, so the shadowing is harmless, but qualify as std::clock_t if you ever need it.

Constructor & Destructor Documentation

◆ timer_t()

cli::timer_t::timer_t ( std::string title = "Timer",
time_print_t time_print = simple )
inlineexplicit

Starts a timer.

Parameters
titleLabel reported alongside the measurement.
time_printCallable used to render the finished measurement.

Member Function Documentation

◆ big()

auto cli::timer_t::big ( std::string title,
std::string time ) -> std::string
inlinestaticnodiscard

Renders a measurement inside a full-width banner.

Parameters
titleLabel for the measurement.
timePreformatted duration.
Returns
The rendered multi-line measurement.

◆ make_time_str() [1/2]

auto cli::timer_t::make_time_str ( ) const -> std::string
inlinenodiscard

Formats the time elapsed since construction, divided by the cycle count.

Returns
The elapsed time with an automatically chosen unit.

◆ make_time_str() [2/2]

auto cli::timer_t::make_time_str ( double time) const -> std::string
inlinenodiscard

Formats a duration, choosing a unit that keeps the value readable.

Parameters
timeA duration in seconds.
Returns
The duration rendered to five significant digits, with a unit.

◆ operator/()

auto cli::timer_t::operator/ ( std::size_t val) -> timer_t &
inline

Sets the divisor applied to the measured interval.

Parameters
valNumber of cycles the interval represents.
Returns
A reference to this timer, for chaining.

◆ simple()

auto cli::timer_t::simple ( std::string title,
std::string time ) -> std::string
inlinestaticnodiscard

Renders a measurement as "title: time".

Parameters
titleLabel for the measurement.
timePreformatted duration.
Returns
The rendered single-line measurement.

◆ time_it()

template<std::invocable F>
auto cli::timer_t::time_it ( F && f,
double target_time = 1.0 ) -> std::string
inlinenodiscard

Repeatedly invokes f until target_time has elapsed.

Runs f at least once and at most 100 times, stopping early once the accumulated time reaches target_time. The timer's own start point is restored before returning, so calling this does not disturb to_string.

Template Parameters
FAny callable invocable with no arguments.
Parameters
fThe callable to time.
target_timeSeconds to keep repeating for.
Returns
A description of the mean time per call and the number of calls.

◆ to_string()

auto cli::timer_t::to_string ( ) const -> std::string
inlinenodiscard

Renders the measurement through the configured print callable.

Returns
The fully rendered measurement.

The documentation for this class was generated from the following file:
  • /home/mccakit/desktop/repositories/cli11/src/timer.cpp