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")
|
| | 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.
|
| |