UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
TicToc.hxx
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*\
2 | |
3 | Copyright (C) 2017 |
4 | |
5 | , __ , __ |
6 | /|/ \ /|/ \ |
7 | | __/ _ ,_ | __/ _ ,_ |
8 | | \|/ / | | | | \|/ / | | | |
9 | |(__/|__/ |_/ \_/|/|(__/|__/ |_/ \_/|/ |
10 | /| /| |
11 | \| \| |
12 | |
13 | Enrico Bertolazzi |
14 | Dipartimento di Ingegneria Industriale |
15 | Università degli Studi di Trento |
16 | Via Sommarive 9, I-38123 Povo, Trento, Italy |
17 | email: enrico.bertolazzi@unitn.it |
18 | |
19\*--------------------------------------------------------------------------*/
20
21//
22// file: TicToc.hxx
23//
24
25#ifndef DOXYGEN_SHOULD_SKIP_THIS
26
27#ifndef UTILS_OS_WINDOWS
28 #include <chrono>
29 #include <thread>
30#endif
31
32#endif
33
34namespace Utils {
35
61
62#ifdef UTILS_OS_WINDOWS
63 class TicToc {
64
65 using real_type = double;
66 int64_t m_frequency{0}; // ticks per second
67 int64_t m_t1{0}, m_t2{0}; // ticks
68 real_type m_elapsed_time{0};
69
70 TicToc( TicToc const & ) = delete;
71 TicToc const & operator = ( TicToc const & ) const = delete;
72
73 public:
74
81 TicToc();
82 ~TicToc() {}
83
89 void tic();
90
97 void toc();
98
104 real_type elapsed_s() const { return 1e-3*m_elapsed_time; }
105
111 real_type elapsed_ms() const { return m_elapsed_time; }
112
118 real_type elapsed_mus() const { return 1000*m_elapsed_time; }
119
125 real_type elapsed_ns() const { return 1e6*m_elapsed_time; }
126
127 };
128
137 void sleep_for_seconds( unsigned s );
138
147 void sleep_for_milliseconds( unsigned ms );
148
157 void sleep_for_microseconds( unsigned mus );
158
167 void sleep_for_nanoseconds( unsigned ns );
168
169#else
170 class TicToc {
171
172 using real_type = double;
173 using clock = std::chrono::high_resolution_clock;
174 using elapsed_resolution = std::chrono::microseconds;
175
176 clock::time_point m_start_time;
177 clock::time_point m_stop_time;
178
179 elapsed_resolution m_elapsed_time{0};
180
181 public:
182
183 TicToc( TicToc const & ) = delete;
184 TicToc const & operator = ( TicToc const & ) const = delete;
185
192 TicToc() { this->tic(); }
193
194 ~TicToc() = default;
195
201 void
203 { m_start_time = clock::now(); }
204
211 void
212 toc() {
213 m_stop_time = clock::now();
214 m_elapsed_time = std::chrono::duration_cast<elapsed_resolution>(m_stop_time - m_start_time);
215 }
216
222 real_type elapsed_s() const { return real_type(1e-6*m_elapsed_time.count()); }
223
229 real_type elapsed_ms() const { return real_type(1e-3*m_elapsed_time.count()); }
230
236 real_type elapsed_mus() const { return real_type(m_elapsed_time.count()); }
237
243 real_type elapsed_ns() const { return real_type(1e3*m_elapsed_time.count()); }
244 };
245
254 inline
255 void
256 sleep_for_seconds( unsigned s )
257 { std::this_thread::sleep_for(std::chrono::seconds(s)); }
258
267 inline
268 void
270 { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); }
271
280 inline
281 void
283 { std::this_thread::sleep_for(std::chrono::microseconds(mus)); }
284
293 inline
294 void
296 { std::this_thread::sleep_for(std::chrono::nanoseconds(ns)); }
297
298#endif
299
300}
301
302//
303// eof: TicToc.hxx
304//
A class for timing code execution.
Definition TicToc.hxx:170
TicToc const & operator=(TicToc const &) const =delete
void tic()
Start timing.
Definition TicToc.hxx:202
void toc()
End timing.
Definition TicToc.hxx:212
~TicToc()=default
real_type elapsed_ms() const
Return elapsed time (between tic-toc) in milliseconds.
Definition TicToc.hxx:229
TicToc()
Constructs a TicToc object and starts the timer.
Definition TicToc.hxx:192
real_type elapsed_s() const
Return elapsed time (between tic-toc) in seconds.
Definition TicToc.hxx:222
real_type elapsed_ns() const
Return elapsed time (between tic-toc) in nanoseconds.
Definition TicToc.hxx:243
TicToc(TicToc const &)=delete
real_type elapsed_mus() const
Return elapsed time (between tic-toc) in microseconds.
Definition TicToc.hxx:236
Definition SystemUtils.cc:39
void sleep_for_milliseconds(unsigned ms)
Sleep for a specified number of milliseconds.
Definition TicToc.hxx:269
void sleep_for_seconds(unsigned s)
Sleep for a specified number of seconds.
Definition TicToc.hxx:256
void sleep_for_nanoseconds(unsigned ns)
Sleep for a specified number of nanoseconds.
Definition TicToc.hxx:295
void sleep_for_microseconds(unsigned mus)
Sleep for a specified number of microseconds.
Definition TicToc.hxx:282