ampsci
c++ program for high-precision atomic structure calculations of single-valence systems
Loading...
Searching...
No Matches
ChronoTimer.hpp
1#pragma once
2#include <chrono>
3#include <iomanip> // setprecision
4#include <iostream>
5#include <sstream> // stringstream
6#include <string>
7
9namespace IO {
10
11/* !
12@brief Class that uses std::chrono to easily time code
13@details
14Usage:
15Will automatically time a routine, based on scope.
16Automatically starts timing on construction, and returns total time elapsed when
17timer goes out of scope. Also "fancy" commands to start/stop the timer if need
18be (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
32string, in units of ms,s,mins, or hours (e.g., "1.56 s" or "2.10 hours")
33
34*/
35class ChronoTimer {
36public:
37 ChronoTimer(std::string_view in_name = "");
38 void start();
39 void stop();
40 void reset();
41 void restart();
42 ~ChronoTimer();
43
44 double reading_ms() const;
45 double lap_reading_ms() const;
46 std::string reading_str() const;
47 std::string lap_reading_str() const;
48
49private:
50 std::string name;
51 bool running;
52 std::chrono::high_resolution_clock::time_point tstart{};
53 std::string convertHR(double t) const;
54 double total_time_ms;
55};
56
57//==============================================================================
58inline ChronoTimer::ChronoTimer(std::string_view in_name)
59 : name(in_name), running(false), total_time_ms(0) {
60 start();
61}
62//==============================================================================
63inline ChronoTimer::~ChronoTimer() {
64 if (name != "")
65 std::cout << name << ": T = " << reading_str() << "\n";
66}
67
68//==============================================================================
69inline void ChronoTimer::start() {
70 // note: will over-ride any existing reading!
71 if (running)
72 stop();
73 running = true;
74 tstart = std::chrono::high_resolution_clock::now();
75}
76
77//==============================================================================
78inline void ChronoTimer::stop() {
79 if (!running)
80 return;
81
82 double current_time = lap_reading_ms();
83 running = false; //"turn off" stopwatch
84
85 // update total time
86 total_time_ms += current_time;
87}
88
89//==============================================================================
90inline void ChronoTimer::restart() {
91 reset();
92 start();
93}
94
95//==============================================================================
96inline void ChronoTimer::reset() {
97 running = false;
98 total_time_ms = 0;
99}
100
101//==============================================================================
102inline double ChronoTimer::lap_reading_ms() const
103// Returns value for current riming run (lap)
104// Returns double (milliseconds)
105{
106
107 if (!running)
108 return 0;
109
110 std::chrono::high_resolution_clock::time_point tcurrent =
111 std::chrono::high_resolution_clock::now();
112
113 auto duration =
114 std::chrono::duration_cast<std::chrono::microseconds>(tcurrent - tstart)
115 .count();
116
117 return ((double)duration) * 1.0e-3;
118}
119
120//==============================================================================
121inline double ChronoTimer::reading_ms() const
122// Returns total value for timnig run
123// Returns double (milliseconds)
124{
125 return lap_reading_ms() + total_time_ms;
126}
127//==============================================================================
128
129inline std::string ChronoTimer::reading_str() const {
130 return convertHR(reading_ms());
131}
132//==============================================================================
133inline std::string ChronoTimer::lap_reading_str() const {
134 return convertHR(lap_reading_ms());
135}
136
137//==============================================================================
138inline std::string ChronoTimer::convertHR(double t) const
139// Convers double (in ms) into formmated 2 d.p. string in units of either
140// ms, s, mins, or hours, depending on size.
141{
142 double ot;
143
144 std::string un;
145 if (t < 1000) {
146 ot = t;
147 un = "ms";
148 } else if (t < 60000) {
149 ot = t / 1000.;
150 un = "s";
151 } else if (t < 3600000) {
152 ot = t / 60000.;
153 un = "mins";
154 } else {
155 ot = t / 3600000.;
156 un = "hours";
157 }
158
159 std::stringstream ss;
160 ss << std::fixed << std::setprecision(2) << ot;
161 return ss.str() + " " + un;
162}
163
164} // namespace IO
In-out (timers, profilers, and read/write data)
Definition ChronoTimer.hpp:9