cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
toolbox::utils::stop_watch_timer_t Class Reference

A high-resolution stopwatch timer for measuring elapsed time. More...

#include <timer.hpp>

Public Member Functions

 stop_watch_timer_t (std::string name="default_timer")
 Construct a new stop_watch_timer_t object.
 
void set_name (const std::string &name)
 Set the name of the timer.
 
auto get_name () const -> const std::string &
 Get the name of the timer.
 
void start ()
 Start or resume the timer.
 
void stop ()
 Stop (pause) the timer and accumulate the duration.
 
void reset ()
 Reset the timer to zero.
 
auto elapsed_time_ms () const -> double
 Get the total elapsed time in milliseconds.
 
auto elapsed_time () const -> double
 Get the total elapsed time in seconds.
 
void print_stats () const
 Print the timer statistics to standard output.
 

Detailed Description

A high-resolution stopwatch timer for measuring elapsed time.

This class provides a simple interface for measuring elapsed time with nanosecond precision. It supports starting, stopping, and resetting the timer, as well as retrieving elapsed time in milliseconds or seconds.

// Basic usage
stop_watch_timer_t timer("MyTimer");
timer.start();
// ... perform some operations ...
timer.stop();
double elapsed_ms = timer.elapsed_time_ms();
timer.print_stats();
// Multiple start/stop cycles
timer.start();
// ... first operation ...
timer.stop();
timer.start();
// ... second operation ...
timer.stop();
double total_seconds = timer.elapsed_time();
A high-resolution stopwatch timer for measuring elapsed time.
Definition timer.hpp:41
void stop()
Stop (pause) the timer and accumulate the duration.
auto elapsed_time() const -> double
Get the total elapsed time in seconds.
void start()
Start or resume the timer.

Constructor & Destructor Documentation

◆ stop_watch_timer_t()

toolbox::utils::stop_watch_timer_t::stop_watch_timer_t ( std::string  name = "default_timer")

Construct a new stop_watch_timer_t object.

Parameters
nameThe name of the timer (default: "default_timer")

Member Function Documentation

◆ elapsed_time()

auto toolbox::utils::stop_watch_timer_t::elapsed_time ( ) const -> double

Get the total elapsed time in seconds.

Returns
double The elapsed time in seconds
Note
This includes all accumulated time between start/stop cycles

◆ elapsed_time_ms()

auto toolbox::utils::stop_watch_timer_t::elapsed_time_ms ( ) const -> double

Get the total elapsed time in milliseconds.

Returns
double The elapsed time in milliseconds
Note
This includes all accumulated time between start/stop cycles

◆ get_name()

auto toolbox::utils::stop_watch_timer_t::get_name ( ) const -> const std::string &

Get the name of the timer.

Returns
const std::string& The current name of the timer

◆ print_stats()

void toolbox::utils::stop_watch_timer_t::print_stats ( ) const

Print the timer statistics to standard output.

Prints the timer name and elapsed time in a human-readable format

◆ reset()

void toolbox::utils::stop_watch_timer_t::reset ( )

Reset the timer to zero.

Note
This also stops the timer if it was running

◆ set_name()

void toolbox::utils::stop_watch_timer_t::set_name ( const std::string &  name)

Set the name of the timer.

Parameters
nameThe new name for the timer

◆ start()

void toolbox::utils::stop_watch_timer_t::start ( )

Start or resume the timer.

Note
If the timer is already running, this will restart the timing

◆ stop()

void toolbox::utils::stop_watch_timer_t::stop ( )

Stop (pause) the timer and accumulate the duration.

Note
If the timer is not running, this function does nothing

The documentation for this class was generated from the following file: