ampsci
High-precision calculations for one- and two-valence atomic systems
IO::ChronoTimer

ok

Class that uses std::chrono to easily time code.

Usage: Will automatically time a routine, based on scope. Automatically starts timing on construction, and returns total time elapsed when timer goes out of scope. Also "fancy" commands to start/stop the timer if need be (e.g., to not time a certain part of the code)

  • sw.start() – starts timing
  • sw.stop() – 'pauses' timing
  • sw.reset() – clears timer. Needs to be re-stared. Forgets all times.
  • start/stop lets you time indevidual sections of code, while not timing others.
  • Calling start() again will start a new "lap", and save current to total.
  • reading_ms() – returns total elapsed time as double, in ms
  • lap_reading_ms() – same, but only returns time since last start()
  • reading_str() and lap_reading_str() – As above, but outputs as formatted string, in units of ms,s,mins, or hours (e.g., "1.56 s" or "2.10 hours")

#include <ChronoTimer.hpp>

Public Member Functions

 ChronoTimer (std::string_view in_name="")
 Constructs and immediately starts the timer. If in_name is non-empty, prints elapsed time to stdout on destruction.
 
void start ()
 Starts (or resumes) timing.
 
void stop ()
 Pauses timing and accumulates elapsed time into the running total.
 
void reset ()
 Clears all accumulated time and stops the timer. Must call start() to resume.
 
void restart ()
 Equivalent to reset() followed by start().
 
 ~ChronoTimer ()
 Prints elapsed time if a name was given at construction.
 
double reading_ms () const
 Total elapsed time in milliseconds (accumulated laps + current lap if running).
 
double lap_reading_ms () const
 Elapsed time in milliseconds for the current lap only (since last start()).
 
std::string reading_str () const
 Total elapsed time as a human-readable string (e.g., "1.56 s", "2.10 hours").
 
std::string lap_reading_str () const
 Current lap elapsed time as a human-readable string.
 

Constructor & Destructor Documentation

◆ ChronoTimer()

IO::ChronoTimer::ChronoTimer ( std::string_view  in_name = "")
inline

Constructs and immediately starts the timer. If in_name is non-empty, prints elapsed time to stdout on destruction.

◆ ~ChronoTimer()

IO::ChronoTimer::~ChronoTimer ( )
inline

Prints elapsed time if a name was given at construction.

Member Function Documentation

◆ start()

void IO::ChronoTimer::start ( )
inline

Starts (or resumes) timing.

If the timer is already running, the current lap is saved to the total before restarting – equivalent to calling stop() then start().

◆ stop()

void IO::ChronoTimer::stop ( )
inline

Pauses timing and accumulates elapsed time into the running total.

◆ reset()

void IO::ChronoTimer::reset ( )
inline

Clears all accumulated time and stops the timer. Must call start() to resume.

◆ restart()

void IO::ChronoTimer::restart ( )
inline

Equivalent to reset() followed by start().

◆ reading_ms()

double IO::ChronoTimer::reading_ms ( ) const
inline

Total elapsed time in milliseconds (accumulated laps + current lap if running).

◆ lap_reading_ms()

double IO::ChronoTimer::lap_reading_ms ( ) const
inline

Elapsed time in milliseconds for the current lap only (since last start()).

◆ reading_str()

std::string IO::ChronoTimer::reading_str ( ) const
inline

Total elapsed time as a human-readable string (e.g., "1.56 s", "2.10 hours").

◆ lap_reading_str()

std::string IO::ChronoTimer::lap_reading_str ( ) const
inline

Current lap elapsed time as a human-readable string.


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