Documentation for OpenFLUID 2.2.2
Loading...
Searching...
No Matches
Timer.hpp
Go to the documentation of this file.
1/*
2
3 This file is part of OpenFLUID software
4 Copyright(c) 2021-2026, INRAE
5
6
7 == GNU General Public License Usage ==
8
9 OpenFLUID is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 OpenFLUID is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21
22
23 == Other Usage ==
24
25 Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26 license, and requires a written agreement between You and INRA.
27 Licensees for Other Usage of OpenFLUID may use this file in accordance
28 with the terms contained in the written agreement between You and INRA.
29
30*/
31
32
33/**
34 @file Timer.hpp
35
36 @author Jean-Christophe FABRE <jean-christophe.fabre@inrae.fr>
37*/
38
39
40#ifndef __OPENFLUID_TOOLS_TIMER_HPP__
41#define __OPENFLUID_TOOLS_TIMER_HPP__
42
43
44#include <chrono>
45#include <string>
46#include <iomanip>
47
49
50
51namespace openfluid { namespace tools {
52
53/**
54 Class to instanciate timers and measure durations between their starts and stops.
55 @snippet misc/timer.cpp usage
56*/
57class Timer
58{
59 private:
60
61 typedef std::chrono::system_clock TimerClock;
62
63 std::chrono::time_point<TimerClock> m_StartTime;
64 std::chrono::time_point<TimerClock> m_EndTime;
65
66 bool m_Running;
67
68
69 public:
70
71 /**
72 Constructs a timer (not started by default)
73 @param[in] AutoStart If true, the timer is automatically started when it is instanciated (false by default)
74 */
75 Timer(bool AutoStart = false):
76 m_Running(false)
77 {
78 if (AutoStart)
79 {
80 start();
81 }
82 }
83
84 /**
85 Starts the timer (if not currently running)
86 */
87 void start()
88 {
89 if (!m_Running)
90 {
91 m_Running = true;
92 m_StartTime = TimerClock::now();
93 }
94 }
95
96
97 /**
98 Stops the timer (if currently running)
99 */
100 void stop()
101 {
102 if (m_Running)
103 {
104 m_EndTime = TimerClock::now();
105 m_Running = false;
106 }
107 }
108
109
110 /**
111 Restarts the timer (even if currently running)
112 */
113 void restart()
114 {
115 m_Running = true;
116 m_EndTime = std::chrono::time_point<TimerClock>();
117 m_StartTime = TimerClock::now();
118 }
119
120
121 /**
122 Stops and resets the timer. If the timer is currently running, the elapsed time is lost
123 */
124 void reset()
125 {
126 m_StartTime = std::chrono::time_point<TimerClock>();
127 m_EndTime = std::chrono::time_point<TimerClock>();
128 m_Running = false;
129 }
130
131
132 /**
133 Returns true if the timer is currently running
134 @return True if timer is running
135 */
137 {
138 return m_Running;
139 }
140
141
142 /**
143 Returns the duration of the timer in milliseconds.
144 If the timer is stopped, it returns the duration between start and stop.
145 If the timer is running, it returns the duration between start and the call of the elapsed() method.
146 @return True if timer is running
147 */
148 long int elapsed()
149 {
150 std::chrono::time_point<TimerClock> EndTime;
151
152 if(m_Running)
153 {
154 EndTime = TimerClock::now();
155 }
156 else
157 {
158 EndTime = m_EndTime;
159 }
160
161 return std::chrono::duration_cast<std::chrono::milliseconds>(EndTime - m_StartTime).count();
162 }
163
164
165 /**
166 Returns the elapsed time as a pretty string representing days, hours, minutes and decimal seconds.
167 @return the converted duration as a string
168 */
170 {
172 }
173
174
175 /**
176 Splits the given duration in milliseconds into days, hours, minutes, seconds and milliseconds.
177 @param[in] MSecsDuration the duration in milliseconds
178 @param[out] Days the number of days
179 @param[out] Hours the number of hours
180 @param[out] Minutes the number of minutes
181 @param[out] Seconds the number of seconds
182 @param[out] MSecs the number of milliseconds
183 */
184 static void splitDuration(long int MSecsDuration, int& Days, int& Hours, int& Minutes, int& Seconds, int& MSecs)
185 {
186 MSecs = (int) (MSecsDuration % 1000);
187 Seconds = (int) (MSecsDuration / 1000) % 60;
188 Minutes = (int) ((MSecsDuration / (1000*60)) % 60);
189 Hours = (int) ((MSecsDuration / (1000*60*60)) % 24);
190 Days = (int) (MSecsDuration / (1000*60*60*24));
191 }
192
193
194 /**
195 Returns the given duration as a pretty string representing days, hours, minutes and decimal seconds.
196 @snippet misc/timer.cpp durationstr
197 @param[in] MSecsDuration the duration in milliseconds
198 @return the converted duration as a string
199 */
200 static std::string getDurationAsPrettyString(long int MSecsDuration)
201 {
202 int MSecs, Seconds, Minutes, Hours, Days = 0;
203
204 splitDuration(MSecsDuration,Days,Hours,Minutes,Seconds,MSecs);
205
206 std::stringstream MSecsSS;
207 MSecsSS << std::setw(3) << std::setfill('0') << MSecs;
208
209 std::string TmpStr = MSecsSS.str()+"s";
210
211 if (!Days)
212 {
213 if (!Hours)
214 {
215 if (!Minutes)
216 {
217 if (!Seconds)
218 {
219 TmpStr = "0."
220 +TmpStr;
221 }
222 else
223 {
224 TmpStr = std::to_string(Seconds)+"."
225 +TmpStr;
226 }
227 }
228 else
229 {
230 TmpStr = std::to_string(Minutes)+"m "+std::to_string(Seconds)+"."
231 +TmpStr;
232 }
233 }
234 else
235 {
236 TmpStr = std::to_string(Hours)+"h "+std::to_string(Minutes)+"m "+std::to_string(Seconds)+"."
237 +TmpStr;
238 }
239 }
240 else
241 {
242 TmpStr = std::to_string(Days)+"d "+std::to_string(Hours)+"h "+
243 std::to_string(Minutes)+"m "+
244 std::to_string(Seconds)+"."
245 +TmpStr;
246 }
247
248 return TmpStr;
249 }
250};
251
252
253} } // namespaces
254
255
256#endif /* __OPENFLUID_TOOLS_TIMER_HPP__ */
Definition Timer.hpp:58
static std::string getDurationAsPrettyString(long int MSecsDuration)
Definition Timer.hpp:200
static void splitDuration(long int MSecsDuration, int &Days, int &Hours, int &Minutes, int &Seconds, int &MSecs)
Definition Timer.hpp:184
void stop()
Definition Timer.hpp:100
long int elapsed()
Definition Timer.hpp:148
std::string elapsedAsPrettyString()
Definition Timer.hpp:169
void start()
Definition Timer.hpp:87
void restart()
Definition Timer.hpp:113
Timer(bool AutoStart=false)
Definition Timer.hpp:75
void reset()
Definition Timer.hpp:124
bool isRunning()
Definition Timer.hpp:136
Definition ApplicationException.hpp:47