Skip to content

Commit 4bf736f

Browse files
committed
Add cxxtimer header only libraries
https://github.com/andremaravilha/cxxtimer Commit: 7d208ebba79e85da8efae5a4b8996846a68bee0f
1 parent c6be8cf commit 4bf736f

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed

src/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ BITCOIN_CORE_H = \
110110
core_memusage.h \
111111
cuckoocache.h \
112112
ctpl.h \
113+
cxxtimer.hpp \
113114
evo/evodb.h \
114115
evo/specialtx.h \
115116
evo/providertx.h \

src/cxxtimer.hpp

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

0 commit comments

Comments
 (0)