Mercurial > code
comparison modules/elapsed-timer/elapsed-timer.hpp @ 532:33f98fda1884
ElapsedTimer: import
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 02 Jun 2016 16:56:44 +0200 |
parents | |
children | f48bb09bccc7 |
comparison
equal
deleted
inserted
replaced
531:25e6380d0849 | 532:33f98fda1884 |
---|---|
1 /* | |
2 * elapsed-timer.hpp -- measure elapsed time | |
3 * | |
4 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #ifndef ELAPSED_TIMER_HPP | |
20 #define ELAPSED_TIMER_HPP | |
21 | |
22 /** | |
23 * \file elapsed-timer.hpp | |
24 * \brief Measure elapsed time | |
25 */ | |
26 | |
27 #include <chrono> | |
28 | |
29 /** | |
30 * \brief Measure elapsed time | |
31 * | |
32 * This class provides an abstraction to measure elapsed time since the construction of the object. It uses | |
33 * std::chrono::high_resolution_clock for more precision and uses milliseconds only. | |
34 */ | |
35 class ElapsedTimer { | |
36 private: | |
37 using TimePoint = std::chrono::time_point<std::chrono::high_resolution_clock>; | |
38 | |
39 TimePoint m_last; | |
40 bool m_paused{false}; | |
41 unsigned m_elapsed{0}; | |
42 | |
43 public: | |
44 /** | |
45 * Construct the elapsed timer, start counting. | |
46 */ | |
47 inline ElapsedTimer() noexcept | |
48 : m_last(std::chrono::high_resolution_clock::now()) | |
49 { | |
50 } | |
51 | |
52 /** | |
53 * Put the timer on pause, the already elapsed time is stored. | |
54 */ | |
55 inline void pause() noexcept | |
56 { | |
57 /* | |
58 * When we put the timer on pause, do not forget to set the already | |
59 * elapsed time. | |
60 */ | |
61 elapsed(); | |
62 m_paused = true; | |
63 } | |
64 | |
65 /** | |
66 * Restart the timer, does not reset it. | |
67 */ | |
68 inline void restart() noexcept | |
69 { | |
70 m_paused = false; | |
71 m_last = std::chrono::high_resolution_clock::now(); | |
72 } | |
73 | |
74 /** | |
75 * Reset the timer to 0. | |
76 */ | |
77 inline void reset() noexcept | |
78 { | |
79 m_elapsed = 0; | |
80 m_last = std::chrono::high_resolution_clock::now(); | |
81 } | |
82 | |
83 /** | |
84 * Get the number of elapsed milliseconds. | |
85 * | |
86 * \return the milliseconds | |
87 */ | |
88 inline unsigned elapsed() noexcept | |
89 { | |
90 using std::chrono::duration_cast; | |
91 using std::chrono::high_resolution_clock; | |
92 using std::chrono::milliseconds; | |
93 | |
94 if (!m_paused) { | |
95 m_elapsed += duration_cast<milliseconds>(high_resolution_clock::now() - m_last).count(); | |
96 m_last = high_resolution_clock::now(); | |
97 } | |
98 | |
99 return m_elapsed; | |
100 } | |
101 }; | |
102 | |
103 #endif // !ELAPSED_TIMER_HPP |