ampsci
High-precision calculations for one- and two-valence atomic systems
ChronoTimer.hpp
1#pragma once
2#include <chrono>
3#include <iomanip> // setprecision
4#include <iostream>
5#include <sstream> // stringstream
6#include <string>
7
8//! In-out (timers, profilers, and read/write data)
9namespace IO {
10
11/*!
12 @brief Class that uses std::chrono to easily time code
13 @details
14 Usage:
15 Will automatically time a routine, based on scope.
16 Automatically starts timing on construction, and returns total time elapsed when
17 timer goes out of scope. Also "fancy" commands to start/stop the timer if need
18 be (e.g., to not time a certain part of the code)
19
20 - sw.start() -- starts timing
21 - sw.stop() -- 'pauses' timing
22 - sw.reset() -- clears timer. Needs to be re-stared. Forgets all times.
23
24 - start/stop lets you time indevidual sections of code, while not timing
25 others.
26 - Calling start() again will start a new "lap", and save current to total.
27
28 - reading_ms() -- returns total elapsed time as double, in ms
29 - lap_reading_ms() -- same, but only returns time since last start()
30
31 - reading_str() and lap_reading_str() -- As above, but outputs as formatted
32 string, in units of ms,s,mins, or hours (e.g., "1.56 s" or "2.10 hours")
33
34*/
36public:
37 //! Constructs and immediately starts the timer. If @p in_name is non-empty,
38 //! prints elapsed time to stdout on destruction.
39 ChronoTimer(std::string_view in_name = "");
40
41 /*!
42 @brief Starts (or resumes) timing.
43 @details
44 If the timer is already running, the current lap is saved to the total
45 before restarting -- equivalent to calling stop() then start().
46 */
47 void start();
48
49 //! Pauses timing and accumulates elapsed time into the running total.
50 void stop();
51
52 //! Clears all accumulated time and stops the timer. Must call start() to resume.
53 void reset();
54
55 //! Equivalent to reset() followed by start().
56 void restart();
57
58 //! Prints elapsed time if a name was given at construction.
60
61 //! Total elapsed time in milliseconds (accumulated laps + current lap if running).
62 double reading_ms() const;
63
64 //! Elapsed time in milliseconds for the current lap only (since last start()).
65 double lap_reading_ms() const;
66
67 //! Total elapsed time as a human-readable string (e.g., "1.56 s", "2.10 hours").
68 std::string reading_str() const;
69
70 //! Current lap elapsed time as a human-readable string.
71 std::string lap_reading_str() const;
72
73private:
74 std::string name;
75 bool running;
76 std::chrono::high_resolution_clock::time_point tstart{};
77 std::string convertHR(double t) const;
78 double total_time_ms;
79};
80
81//==============================================================================
82inline ChronoTimer::ChronoTimer(std::string_view in_name)
83 : name(in_name), running(false), total_time_ms(0) {
84 start();
85}
86//==============================================================================
88 if (name != "")
89 std::cout << name << ": T = " << reading_str() << "\n";
90}
91
92//==============================================================================
93inline void ChronoTimer::start() {
94 // note: will over-ride any existing reading!
95 if (running)
96 stop();
97 running = true;
98 tstart = std::chrono::high_resolution_clock::now();
99}
100
101//==============================================================================
102inline void ChronoTimer::stop() {
103 if (!running)
104 return;
105
106 double current_time = lap_reading_ms();
107 running = false; //"turn off" stopwatch
108
109 // update total time
110 total_time_ms += current_time;
111}
112
113//==============================================================================
114inline void ChronoTimer::restart() {
115 reset();
116 start();
117}
118
119//==============================================================================
120inline void ChronoTimer::reset() {
121 running = false;
122 total_time_ms = 0;
123}
124
125//==============================================================================
126inline double ChronoTimer::lap_reading_ms() const
127// Returns value for current riming run (lap)
128// Returns double (milliseconds)
129{
130
131 if (!running)
132 return 0;
133
134 std::chrono::high_resolution_clock::time_point tcurrent =
135 std::chrono::high_resolution_clock::now();
136
137 auto duration =
138 std::chrono::duration_cast<std::chrono::microseconds>(tcurrent - tstart)
139 .count();
140
141 return ((double)duration) * 1.0e-3;
142}
143
144//==============================================================================
145inline double ChronoTimer::reading_ms() const
146// Returns total value for timnig run
147// Returns double (milliseconds)
148{
149 return lap_reading_ms() + total_time_ms;
150}
151//==============================================================================
152
153inline std::string ChronoTimer::reading_str() const {
154 return convertHR(reading_ms());
155}
156//==============================================================================
157inline std::string ChronoTimer::lap_reading_str() const {
158 return convertHR(lap_reading_ms());
159}
160
161//==============================================================================
162inline std::string ChronoTimer::convertHR(double t) const
163// Convers double (in ms) into formmated 2 d.p. string in units of either
164// ms, s, mins, or hours, depending on size.
165{
166 double ot;
167
168 std::string un;
169 if (t < 1000) {
170 ot = t;
171 un = "ms";
172 } else if (t < 60000) {
173 ot = t / 1000.;
174 un = "s";
175 } else if (t < 3600000) {
176 ot = t / 60000.;
177 un = "mins";
178 } else {
179 ot = t / 3600000.;
180 un = "hours";
181 }
182
183 std::stringstream ss;
184 ss << std::fixed << std::setprecision(2) << ot;
185 return ss.str() + " " + un;
186}
187
188} // namespace IO
Class that uses std::chrono to easily time code.
Definition ChronoTimer.hpp:35
std::string lap_reading_str() const
Current lap elapsed time as a human-readable string.
Definition ChronoTimer.hpp:157
void start()
Starts (or resumes) timing.
Definition ChronoTimer.hpp:93
ChronoTimer(std::string_view in_name="")
Constructs and immediately starts the timer. If in_name is non-empty, prints elapsed time to stdout o...
Definition ChronoTimer.hpp:82
double lap_reading_ms() const
Elapsed time in milliseconds for the current lap only (since last start()).
Definition ChronoTimer.hpp:126
~ChronoTimer()
Prints elapsed time if a name was given at construction.
Definition ChronoTimer.hpp:87
void restart()
Equivalent to reset() followed by start().
Definition ChronoTimer.hpp:114
std::string reading_str() const
Total elapsed time as a human-readable string (e.g., "1.56 s", "2.10 hours").
Definition ChronoTimer.hpp:153
void stop()
Pauses timing and accumulates elapsed time into the running total.
Definition ChronoTimer.hpp:102
void reset()
Clears all accumulated time and stops the timer. Must call start() to resume.
Definition ChronoTimer.hpp:120
double reading_ms() const
Total elapsed time in milliseconds (accumulated laps + current lap if running).
Definition ChronoTimer.hpp:145
In-out (timers, profilers, and read/write data)
Definition ChronoTimer.hpp:9