Skip to content

Commit a99bdd8

Browse files
committed
[Utils] Add tiny header-only timer class
From https://github.com/andremaravilha/cxxtimer commit: a48bd81d9168a4e258123a84fc150912dd65ce79
1 parent 90dd6ed commit a99bdd8

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ BITCOIN_CORE_H = \
163163
clientversion.h \
164164
coincontrol.h \
165165
coins.h \
166+
cxxtimer.hpp \
166167
compat.h \
167168
compat/byteswap.h \
168169
compat/cpuid.h \

src/cxxtimer.hpp

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
MIT License
3+
Copyright (c) 2017 André L. Maravilha
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18+
SOFTWARE.
19+
*/
20+
21+
/*
22+
* https://github.com/andremaravilha/cxxtimer
23+
* commit: a48bd81d9168a4e258123a84fc150912dd65ce79
24+
*/
25+
26+
#ifndef CXX_TIMER_HPP
27+
#define CXX_TIMER_HPP
28+
29+
#include <chrono>
30+
31+
32+
namespace cxxtimer {
33+
34+
/**
35+
* This class works as a stopwatch.
36+
*/
37+
class Timer {
38+
39+
public:
40+
41+
/**
42+
* Constructor.
43+
*
44+
* @param start
45+
* If true, the timer is started just after construction.
46+
* Otherwise, it will not be automatically started.
47+
*/
48+
Timer(bool start = false);
49+
50+
/**
51+
* Copy constructor.
52+
*
53+
* @param other
54+
* The object to be copied.
55+
*/
56+
Timer(const Timer& other) = default;
57+
58+
/**
59+
* Transfer constructor.
60+
*
61+
* @param other
62+
* The object to be transfered.
63+
*/
64+
Timer(Timer&& other) = default;
65+
66+
/**
67+
* Destructor.
68+
*/
69+
virtual ~Timer() = default;
70+
71+
/**
72+
* Assignment operator by copy.
73+
*
74+
* @param other
75+
* The object to be copied.
76+
*
77+
* @return A reference to this object.
78+
*/
79+
Timer& operator=(const Timer& other) = default;
80+
81+
/**
82+
* Assignment operator by transfer.
83+
*
84+
* @param other
85+
* The object to be transferred.
86+
*
87+
* @return A reference to this object.
88+
*/
89+
Timer& operator=(Timer&& other) = default;
90+
91+
/**
92+
* Start/resume the timer.
93+
*/
94+
void start();
95+
96+
/**
97+
* Stop/pause the timer.
98+
*/
99+
void stop();
100+
101+
/**
102+
* Reset the timer.
103+
*/
104+
void reset();
105+
106+
/**
107+
* Return the elapsed time.
108+
*
109+
* @param duration_t
110+
* The duration type used to return the time elapsed. If not
111+
* specified, it returns the time as represented by
112+
* std::chrono::milliseconds.
113+
*
114+
* @return The elapsed time.
115+
*/
116+
template <class duration_t = std::chrono::milliseconds>
117+
typename duration_t::rep count() const;
118+
119+
private:
120+
121+
bool started_;
122+
bool paused_;
123+
std::chrono::steady_clock::time_point reference_;
124+
std::chrono::duration<long double> accumulated_;
125+
};
126+
127+
}
128+
129+
130+
inline cxxtimer::Timer::Timer(bool start) :
131+
started_(false), paused_(false),
132+
reference_(std::chrono::steady_clock::now()),
133+
accumulated_(std::chrono::duration<long double>(0)) {
134+
if (start) {
135+
this->start();
136+
}
137+
}
138+
139+
inline void cxxtimer::Timer::start() {
140+
if (!started_) {
141+
started_ = true;
142+
paused_ = false;
143+
accumulated_ = std::chrono::duration<long double>(0);
144+
reference_ = std::chrono::steady_clock::now();
145+
} else if (paused_) {
146+
reference_ = std::chrono::steady_clock::now();
147+
paused_ = false;
148+
}
149+
}
150+
151+
inline void cxxtimer::Timer::stop() {
152+
if (started_ && !paused_) {
153+
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
154+
accumulated_ = accumulated_ + std::chrono::duration_cast< std::chrono::duration<long double> >(now - reference_);
155+
paused_ = true;
156+
}
157+
}
158+
159+
inline void cxxtimer::Timer::reset() {
160+
if (started_) {
161+
started_ = false;
162+
paused_ = false;
163+
reference_ = std::chrono::steady_clock::now();
164+
accumulated_ = std::chrono::duration<long double>(0);
165+
}
166+
}
167+
168+
template <class duration_t>
169+
typename duration_t::rep cxxtimer::Timer::count() const {
170+
if (started_) {
171+
if (paused_) {
172+
return std::chrono::duration_cast<duration_t>(accumulated_).count();
173+
} else {
174+
return std::chrono::duration_cast<duration_t>(
175+
accumulated_ + (std::chrono::steady_clock::now() - reference_)).count();
176+
}
177+
} else {
178+
return duration_t(0).count();
179+
}
180+
}
181+
182+
183+
#endif

0 commit comments

Comments
 (0)