DART 6.13.2
Loading...
Searching...
No Matches
Stopwatch-impl.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2022, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
34
35namespace dart::common {
36
37namespace {
38//==============================================================================
39template <class duration_t>
40void print_duration_if_non_zero(
41 std::ostream& os,
42 const duration_t& duration,
43 const std::string& unit_postfix)
44{
45 if (duration == duration_t::zero())
46 {
47 return;
48 }
49
50 os << duration.count() << unit_postfix;
51}
52} // namespace
53
54//==============================================================================
55template <typename UnitType, typename ClockType>
57 : mStart(ClockType::now()), mElapsed(0), mPaused(!start)
58{
59 // clang-format off
60 static_assert(std::is_same_v<UnitType, std::chrono::seconds>
61 || std::is_same_v<UnitType, std::chrono::milliseconds>
62 || std::is_same_v<UnitType, std::chrono::microseconds>
63 || std::is_same_v<UnitType, std::chrono::nanoseconds>,
64 "Invalid unit");
65 // clang-format on
66}
67
68//==============================================================================
69template <typename UnitType, typename ClockType>
74
75//==============================================================================
76template <typename UnitType, typename ClockType>
78{
79 return !mPaused;
80}
81
82//==============================================================================
83template <typename UnitType, typename ClockType>
85{
86 if (!mPaused)
87 {
88 return;
89 }
90
91 mStart = ClockType::now();
92 mPaused = false;
93}
94
95//==============================================================================
96template <typename UnitType, typename ClockType>
98{
99 if (mPaused)
100 {
101 return;
102 }
103
104 mElapsed += std::chrono::duration_cast<UnitType>(ClockType::now() - mStart);
105 mPaused = true;
106}
107
108//==============================================================================
109template <typename UnitType, typename ClockType>
111{
112 mStart = ClockType::now();
113 mElapsed = UnitType(0);
114}
115
116//==============================================================================
117template <typename UnitType, typename ClockType>
119{
120 if constexpr (std::is_same_v<UnitType, std::chrono::nanoseconds>)
121 {
122 return duration().count() * 1e-9;
123 }
124 else if constexpr (std::is_same_v<UnitType, std::chrono::microseconds>)
125 {
126 return duration().count() * 1e-6;
127 }
128 else if constexpr (std::is_same_v<UnitType, std::chrono::milliseconds>)
129 {
130 return duration().count() * 1e-3;
131 }
132 else if constexpr (std::is_same_v<UnitType, std::chrono::seconds>)
133 {
134 return duration().count();
135 }
136}
137
138//==============================================================================
139template <typename UnitType, typename ClockType>
141{
142 if constexpr (std::is_same_v<UnitType, std::chrono::nanoseconds>)
143 {
144 return duration().count() * 1e-6;
145 }
146 else if constexpr (std::is_same_v<UnitType, std::chrono::microseconds>)
147 {
148 return duration().count() * 1e-3;
149 }
150 else if constexpr (std::is_same_v<UnitType, std::chrono::milliseconds>)
151 {
152 return duration().count();
153 }
154 else if constexpr (std::is_same_v<UnitType, std::chrono::seconds>)
155 {
156 return duration().count() * 1e+3;
157 ;
158 }
159}
160
161//==============================================================================
162template <typename UnitType, typename ClockType>
164{
165 if constexpr (std::is_same_v<UnitType, std::chrono::nanoseconds>)
166 {
167 return duration().count() * 1e-3;
168 }
169 else if constexpr (std::is_same_v<UnitType, std::chrono::microseconds>)
170 {
171 return duration().count();
172 }
173 else if constexpr (std::is_same_v<UnitType, std::chrono::milliseconds>)
174 {
175 return duration().count() * 1e+3;
176 }
177 else if constexpr (std::is_same_v<UnitType, std::chrono::seconds>)
178 {
179 return duration().count() * 1e+6;
180 ;
181 }
182}
183
184//==============================================================================
185template <typename UnitType, typename ClockType>
187{
188 if constexpr (std::is_same_v<UnitType, std::chrono::nanoseconds>)
189 {
190 return duration().count();
191 }
192 else if constexpr (std::is_same_v<UnitType, std::chrono::microseconds>)
193 {
194 return duration().count() * 1e+3;
195 }
196 else if constexpr (std::is_same_v<UnitType, std::chrono::milliseconds>)
197 {
198 return duration().count() * 1e+6;
199 }
200 else if constexpr (std::is_same_v<UnitType, std::chrono::seconds>)
201 {
202 return duration().count() * 1e+9;
203 }
204}
205
206//==============================================================================
207template <typename UnitType, typename ClockType>
209{
210 if (mPaused)
211 {
212 return mElapsed;
213 }
214
215 const auto end = ClockType::now();
216 const auto duration = std::chrono::duration_cast<UnitType>(end - mStart);
217 return duration + mElapsed;
218}
219
220//==============================================================================
221template <typename UnitType, typename ClockType>
222void Stopwatch<UnitType, ClockType>::print(std::ostream& os) const
223{
224 auto t = duration();
225
226 const auto h = std::chrono::duration_cast<std::chrono::hours>(t);
227 print_duration_if_non_zero(os, h, "h ");
228 t -= h;
229
230 const auto m = std::chrono::duration_cast<std::chrono::minutes>(t);
231 print_duration_if_non_zero(os, m, "min ");
232 t -= m;
233
234 const auto s = std::chrono::duration_cast<std::chrono::seconds>(t);
235 print_duration_if_non_zero(os, s, "s ");
236 t -= s;
237
238 if constexpr (!std::is_same_v<UnitType, std::chrono::seconds>)
239 {
240 const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(t);
241 print_duration_if_non_zero(os, ms, "ms ");
242 t -= ms;
243
244 if constexpr (!std::is_same_v<UnitType, std::chrono::milliseconds>)
245 {
246 const auto us = std::chrono::duration_cast<std::chrono::microseconds>(t);
247 print_duration_if_non_zero(os, us, "us ");
248 t -= us;
249
250 if constexpr (!std::is_same_v<UnitType, std::chrono::microseconds>)
251 {
252 const auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(t);
253 print_duration_if_non_zero(os, ns, "ns ");
254 if (us == std::chrono::nanoseconds::zero())
255 {
256 os << "0 ns\n";
257 }
258 }
259 else if (us == std::chrono::microseconds::zero())
260 {
261 os << "0 us\n";
262 }
263 }
264 else if (ms == std::chrono::milliseconds::zero())
265 {
266 os << "0 ms\n";
267 }
268 }
269 else if (s == std::chrono::seconds::zero())
270 {
271 os << "0 s\n";
272 }
273}
274
275//==============================================================================
276template <typename UnitType, typename ClockType>
277std::ostream& operator<<(
278 std::ostream& os, const Stopwatch<UnitType, ClockType>& sw)
279{
280 sw.print(os);
281 return os;
282}
283
284} // namespace dart::common
Simple stopwatch implementation.
Definition Stopwatch.hpp:46
Stopwatch(bool start=true)
Constructor.
Definition Stopwatch-impl.hpp:56
double elapsedMS() const
Returns the elapsed time in milliseconds.
Definition Stopwatch-impl.hpp:140
double elapsedS() const
Returns the elapsed time in seconds.
Definition Stopwatch-impl.hpp:118
void stop()
Stops the stopwatch.
Definition Stopwatch-impl.hpp:97
void start()
Starts the stopwatch.
Definition Stopwatch-impl.hpp:84
void reset()
Resets the stopwatch.
Definition Stopwatch-impl.hpp:110
bool isStarted() const
Returns whether the stopwatch is started.
Definition Stopwatch-impl.hpp:77
void print(std::ostream &os=std::cout) const
Prints state of the stopwatch.
Definition Stopwatch-impl.hpp:222
double elapsedNS() const
Returns the elapsed time in nanoseconds.
Definition Stopwatch-impl.hpp:186
~Stopwatch()
Destructor.
Definition Stopwatch-impl.hpp:70
double elapsedUS() const
Returns the elapsed time in microseconds.
Definition Stopwatch-impl.hpp:163
UnitType duration() const
Returns the duration.
Definition Stopwatch-impl.hpp:208
Definition Aspect.cpp:42
std::ostream & operator<<(std::ostream &os, const StlAllocator< T > &allocator)
Definition StlAllocator-impl.hpp:104