Adobe Source Libraries 1.49.0
A collection of C++ libraries.
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1/*
2 Copyright 2013 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6/**************************************************************************************************/
7
8#ifndef ADOBE_TIMER_HPP
9#define ADOBE_TIMER_HPP
10
11/**************************************************************************************************/
12
88
89/**************************************************************************************************/
90
91#include <adobe/config.hpp>
92
95#include <adobe/numeric.hpp>
96
97#include <boost/operators.hpp>
98
99#if ADOBE_PLATFORM_WIN
100#ifndef WINDOWS_LEAN_AND_MEAN
101#define WINDOWS_LEAN_AND_MEAN
102#define ADOBE_UNDEFINE_WINDOWS_LEAN_AND_MEAN 1
103#endif
104#include <windows.h>
105#if ADOBE_UNDEFINE_WINDOWS_LEAN_AND_MEAN
106#undef WINDOWS_LEAN_AND_MEAN
107#undef ADOBE_UNDEFINE_WINDOWS_LEAN_AND_MEAN
108#endif
109#endif
110
111#include <cassert>
112#include <chrono>
113#include <iostream>
114#include <vector>
115
116/**************************************************************************************************/
117
118namespace adobe {
119
120/**************************************************************************************************/
121
122class timer_t : boost::totally_ordered<timer_t> {
123 using value_type = std::chrono::time_point<std::chrono::high_resolution_clock>;
124
125 typedef std::vector<double> accumulator_type;
126
127public:
128 typedef accumulator_type::size_type size_type;
129
130#ifndef ADOBE_NO_DOCUMENTATION
131
133 reset();
134 }
135
136 timer_t(const timer_t& rhs)
137 : epoch_m(rhs.epoch_m), split_m(rhs.split_m), time_set_m(rhs.time_set_m)
138 {
139 }
140
141 timer_t& operator=(const timer_t& rhs) {
142 epoch_m = rhs.epoch_m;
143 split_m = rhs.split_m;
144 time_set_m = rhs.time_set_m;
145
146 return *this;
147 }
148
149#endif
150
154
155 inline void reset() {
156 epoch_m = std::chrono::high_resolution_clock::now();
157 }
158
162
163 inline void reset_accumulator() { time_set_m.clear(); }
164
169
170 inline double split() {
171 const std::chrono::duration<double, std::milli> delta{
172 std::chrono::high_resolution_clock::now() - epoch_m};
173 return delta.count();
174 }
175
179
180 inline void accrue() {
181 time_set_m.push_back(split());
182 reset();
183 }
184
189
190 inline double accrued_min() const { return *adobe::min_element(time_set_m); }
191
196
197 inline double accrued_max() const { return *adobe::max_element(time_set_m); }
198
203
204 inline double accrued_average() const { return empty() ? 0 : accrued_total() / size(); }
205
210
211 inline double accrued_median() const {
212 if (empty())
213 return 0;
214
215 adobe::sort(time_set_m);
216
217 return (size() % 2 == 1)
218 ? time_set_m[time_set_m.size() / 2]
219 : (time_set_m[time_set_m.size() / 2] + time_set_m[time_set_m.size() / 2 - 1]) /
220 2;
221 }
222
227
228 inline double accrued_total() const { return adobe::accumulate(time_set_m, double(0)); }
229
234
235 inline size_type size() const { return time_set_m.size(); }
236
241
242 inline bool empty() const { return time_set_m.empty(); }
243
251
252 inline void report(const char* decoration, std::ostream& s = std::cout) {
253 double time(split());
254
255 s << decoration << " took " << time << " milliseconds (" << time / 1e3 << " sec)"
256 << std::endl;
257
258 reset();
259 }
260
261private:
262#ifndef ADOBE_NO_DOCUMENTATION
263 friend bool operator==(const timer_t& x, const timer_t& y);
264 friend bool operator<(const timer_t& x, const timer_t& y);
265#endif
266
267 value_type epoch_m;
268 value_type split_m;
269 mutable accumulator_type time_set_m;
270};
271
272/**************************************************************************************************/
273
274#ifndef ADOBE_NO_DOCUMENTATION
275
276inline bool operator==(const timer_t& /*x*/, const timer_t& /*y*/) { return true; }
277
278/**************************************************************************************************/
279
280inline bool operator<(const timer_t& /*x*/, const timer_t& /*y*/) { return false; }
281
282#endif
283
284/**************************************************************************************************/
285
286} // namespace adobe
287
288/**************************************************************************************************/
289
290#endif
291
292/**************************************************************************************************/
double accrued_max() const
Definition timer.hpp:197
timer_t & operator=(const timer_t &rhs)
Definition timer.hpp:141
size_type size() const
Definition timer.hpp:235
void accrue()
Definition timer.hpp:180
friend bool operator<(const timer_t &x, const timer_t &y)
Definition timer.hpp:280
bool empty() const
Definition timer.hpp:242
double accrued_median() const
Definition timer.hpp:211
void report(const char *decoration, std::ostream &s=std::cout)
Definition timer.hpp:252
accumulator_type::size_type size_type
Definition timer.hpp:128
double accrued_total() const
Definition timer.hpp:228
void reset_accumulator()
Definition timer.hpp:163
timer_t(const timer_t &rhs)
Definition timer.hpp:136
double split()
Definition timer.hpp:170
void reset()
Definition timer.hpp:155
double accrued_min() const
Definition timer.hpp:190
double accrued_average() const
Definition timer.hpp:204
friend bool operator==(const timer_t &x, const timer_t &y)
Definition timer.hpp:276
T accumulate(const InputRange &range, T init)
Definition numeric.hpp:111
bool operator<(const step_iterator_adaptor< D, IT, S_FN > &p1, const step_iterator_adaptor< D, IT, S_FN > &p2)
Definition iterator.hpp:363
boost::range_iterator< ForwardRange >::type min_element(ForwardRange &range)
minmax implementation
Definition minmax.hpp:157
boost::range_iterator< ForwardRange >::type max_element(ForwardRange &range)
minmax implementation
Definition minmax.hpp:210
void sort(RandomAccessRange &range)
sort implementation
Definition sort.hpp:40
bool operator==(const any_regular_t &x, const any_regular_t &y)