C++ Utilities
Loading...
Searching...
No Matches
utilities::stopwatch< Clock >

See the Stopwatch page for all the details. More...

#include <stopwatch.h>

Public Types

using clock_type = Clock
 The underlying clock type.

Public Member Functions

 stopwatch (const std::string &str="")
 A stopwatch can have a name to distinguish it from others you may have running.
std::string name () const
 Provides read-only access to the stopwatch's name.
std::string & name ()
 Provides read-write access to the stopwatch's name.
void reset ()
 Set/reset the stopwatch's 'zero' point & clear any measured splits.
double elapsed () const
 Get the time that has passed from the zero point to now. Units are seconds.
double click ()
 Clicks the stopwatch to create a new 'split' and returns the elapsed time in seconds.
double split () const
 Returns the split as the time in seconds that elapsed from the zero point to the last click.
double lap () const
 Returns the last 'lap' time in seconds (i.e. the time between prior 2 splits).
std::string to_string () const
 Returns a string representation of the stopwatch's elapsed time in seconds.

Static Public Member Functions

static constexpr std::string format_seconds (double seconds)
 Class method that returns a "pretty" string for a time in seconds.

Detailed Description

template<typename Clock = std::chrono::high_resolution_clock>
class utilities::stopwatch< Clock >

See the Stopwatch page for all the details.

Member Function Documentation

◆ format_seconds()

template<typename Clock = std::chrono::high_resolution_clock>
constexpr std::string utilities::stopwatch< Clock >::format_seconds ( double seconds)
inlinestaticconstexpr

Class method that returns a "pretty" string for a time in seconds.

  • If the time is less than 1 second, it is formatted as a number of milliseconds.
  • If the time is greater than 1 second, it is formatted as a number of seconds.

Numbers are formatted to 2 decimal places and output with a unit suffix (ms or s).

Examples

assert_eq(stopwatch<>::format_seconds(0.0001), "0.10ms");
assert_eq(stopwatch<>::format_seconds(0.011), "11.00ms");
assert_eq(stopwatch<>::format_seconds(1.0), "1.00s");
assert_eq(stopwatch<>::format_seconds(25.23456789), "25.23s");
static constexpr std::string format_seconds(double seconds)
Class method that returns a "pretty" string for a time in seconds.
Definition stopwatch.h:79