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 print is true (default),
38 //! prints elapsed time to stdout on destruction.
39 ChronoTimer(std::string_view in_name = "", bool print = true);
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 m_print;
76 bool running;
77 std::chrono::high_resolution_clock::time_point tstart{};
78 double total_time_ms;
79
80 std::string convertHR(double t) const;
81};
82
83//==============================================================================
84inline ChronoTimer::ChronoTimer(std::string_view in_name, bool print)
85 : name(in_name), m_print(print), running(false), total_time_ms(0) {
86 start();
87}
88//==============================================================================
90 if (!m_print)
91 return;
92 if (name.empty()) {
93 std::cout << reading_str() << "\n";
94 } else {
95 std::cout << name << ": T = " << reading_str() << "\n";
96 }
97}
98
99//==============================================================================
100inline void ChronoTimer::start() {
101 // note: will over-ride any existing reading!
102 if (running)
103 stop();
104 running = true;
105 tstart = std::chrono::high_resolution_clock::now();
106}
107
108//==============================================================================
109inline void ChronoTimer::stop() {
110 if (!running)
111 return;
112
113 double current_time = lap_reading_ms();
114 running = false; //"turn off" stopwatch
115
116 // update total time
117 total_time_ms += current_time;
118}
119
120//==============================================================================
121inline void ChronoTimer::restart() {
122 reset();
123 start();
124}
125
126//==============================================================================
127inline void ChronoTimer::reset() {
128 running = false;
129 total_time_ms = 0;
130}
131
132//==============================================================================
133inline double ChronoTimer::lap_reading_ms() const
134// Returns value for current riming run (lap)
135// Returns double (milliseconds)
136{
137
138 if (!running)
139 return 0;
140
141 std::chrono::high_resolution_clock::time_point tcurrent =
142 std::chrono::high_resolution_clock::now();
143
144 auto duration =
145 std::chrono::duration_cast<std::chrono::microseconds>(tcurrent - tstart)
146 .count();
147
148 return ((double)duration) * 1.0e-3;
149}
150
151//==============================================================================
152inline double ChronoTimer::reading_ms() const
153// Returns total value for timnig run
154// Returns double (milliseconds)
155{
156 return lap_reading_ms() + total_time_ms;
157}
158//==============================================================================
159
160inline std::string ChronoTimer::reading_str() const {
161 return convertHR(reading_ms());
162}
163//==============================================================================
164inline std::string ChronoTimer::lap_reading_str() const {
165 return convertHR(lap_reading_ms());
166}
167
168//==============================================================================
169inline std::string ChronoTimer::convertHR(double t) const
170// Convers double (in ms) into formmated 2 d.p. string in units of either
171// ms, s, mins, or hours, depending on size.
172{
173 double ot;
174
175 std::string un;
176 if (t < 1000) {
177 ot = t;
178 un = "ms";
179 } else if (t < 60000) {
180 ot = t / 1000.;
181 un = "s";
182 } else if (t < 3600000) {
183 ot = t / 60000.;
184 un = "mins";
185 } else {
186 ot = t / 3600000.;
187 un = "hours";
188 }
189
190 std::stringstream ss;
191 ss << std::fixed << std::setprecision(2) << ot;
192 return ss.str() + " " + un;
193}
194
195} // 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:164
void start()
Starts (or resumes) timing.
Definition ChronoTimer.hpp:100
double lap_reading_ms() const
Elapsed time in milliseconds for the current lap only (since last start()).
Definition ChronoTimer.hpp:133
~ChronoTimer()
Prints elapsed time if a name was given at construction.
Definition ChronoTimer.hpp:89
void restart()
Equivalent to reset() followed by start().
Definition ChronoTimer.hpp:121
ChronoTimer(std::string_view in_name="", bool print=true)
Constructs and immediately starts the timer. If print is true (default), prints elapsed time to stdou...
Definition ChronoTimer.hpp:84
std::string reading_str() const
Total elapsed time as a human-readable string (e.g., "1.56 s", "2.10 hours").
Definition ChronoTimer.hpp:160
void stop()
Pauses timing and accumulates elapsed time into the running total.
Definition ChronoTimer.hpp:109
void reset()
Clears all accumulated time and stops the timer. Must call start() to resume.
Definition ChronoTimer.hpp:127
double reading_ms() const
Total elapsed time in milliseconds (accumulated laps + current lap if running).
Definition ChronoTimer.hpp:152
In-out (timers, profilers, and read/write data)
Definition ChronoTimer.hpp:9