stlab.adobe.com Adobe Systems Incorporated
timer.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
4  or a copy at http://stlab.adobe.com/licenses.html)
5 */
6 
7 /****************************************************************************************************/
8 
9 #ifndef ADOBE_TIMER_HPP
10 #define ADOBE_TIMER_HPP
11 
12 /****************************************************************************************************/
13 
90 /****************************************************************************************************/
91 
92 #include <adobe/config.hpp>
93 
95 #include <adobe/algorithm/sort.hpp>
96 #include <adobe/numeric.hpp>
97 
98 #include <boost/operators.hpp>
99 
100 #if ADOBE_PLATFORM_WIN
101  #ifndef WINDOWS_LEAN_AND_MEAN
102  #define WINDOWS_LEAN_AND_MEAN
103  #define ADOBE_UNDEFINE_WINDOWS_LEAN_AND_MEAN 1
104  #endif
105  #include <windows.h>
106  #if ADOBE_UNDEFINE_WINDOWS_LEAN_AND_MEAN
107  #undef WINDOWS_LEAN_AND_MEAN
108  #undef ADOBE_UNDEFINE_WINDOWS_LEAN_AND_MEAN
109  #endif
110 #elif defined(BOOST_HAS_THREADS)
111  #include <boost/thread/xtime.hpp>
112 #elif defined(BOOST_HAS_GETTIMEOFDAY)
113  #include <sys/time.h>
114 #endif
115 
116 #include <iostream>
117 #include <vector>
118 #include <cassert>
119 
120 /****************************************************************************************************/
121 
122 namespace adobe {
123 
124 /****************************************************************************************************/
125 
126 class timer_t : boost::totally_ordered<timer_t>
127 {
128 #if ADOBE_PLATFORM_WIN
129  typedef LARGE_INTEGER value_type;
130 #elif defined(BOOST_HAS_THREADS)
131  typedef boost::xtime value_type;
132 #elif defined(BOOST_HAS_GETTIMEOFDAY)
133  typedef timeval value_type;
134 #endif
135 
136  typedef std::vector<double> accumulator_type;
137 
138 public:
139  typedef accumulator_type::size_type size_type;
140 
141 #ifndef ADOBE_NO_DOCUMENTATION
142 
143  timer_t()
144  {
145 #if ADOBE_PLATFORM_WIN
146  (void)::QueryPerformanceFrequency(&frequency_m);
147 #endif
148 
149  reset();
150  }
151 
152  timer_t(const timer_t& rhs) :
153  epoch_m(rhs.epoch_m),
154  split_m(rhs.split_m),
155  time_set_m(rhs.time_set_m)
156 #if ADOBE_PLATFORM_WIN
157  , frequency_m(rhs.frequency_m)
158 #endif
159  { }
160 
161  timer_t& operator = (const timer_t& rhs)
162  {
163  epoch_m = rhs.epoch_m;
164  split_m = rhs.split_m;
165  time_set_m = rhs.time_set_m;
166 
167 #if ADOBE_PLATFORM_WIN
168  frequency_m = rhs.frequency_m;
169 #endif
170 
171  return *this;
172  }
173 
174 #endif
175 
180  inline void reset()
181  {
182 #if ADOBE_PLATFORM_WIN
183  (void)::QueryPerformanceCounter(&epoch_m);
184 #elif defined(BOOST_HAS_THREADS)
185  boost::xtime_get(&epoch_m, boost::TIME_UTC_);
186 #elif defined(BOOST_HAS_GETTIMEOFDAY)
187  gettimeofday(&epoch_m, static_cast<struct timezone*>(0));
188 #endif
189  }
190 
195  inline void reset_accumulator()
196  { time_set_m.clear(); }
197 
203  inline double split()
204  {
205 #if ADOBE_PLATFORM_WIN
206  (void)::QueryPerformanceCounter(&split_m);
207  return (split_m.QuadPart - epoch_m.QuadPart) / static_cast<double>(frequency_m.QuadPart) * double(1e3);
208 #elif defined(BOOST_HAS_THREADS)
209  boost::xtime_get(&split_m, boost::TIME_UTC_);
210  return ((split_m.sec - epoch_m.sec) * double(1e3) + (split_m.nsec - epoch_m.nsec) / double(1e6));
211 #elif defined(BOOST_HAS_GETTIMEOFDAY)
212  gettimeofday(&split_m, static_cast<struct timezone*>(0));
213  return ((split_m.tv_sec - epoch_m.tv_sec) * double(1e3) + (split_m.tv_usec - epoch_m.tv_usec) / double(1e3));
214 #else
215  return -1;
216 #endif
217 
218  }
219 
224  inline void accrue()
225  { time_set_m.push_back(split()); reset(); }
226 
232  inline double accrued_min() const
233  { return *adobe::min_element(time_set_m); }
234 
240  inline double accrued_max() const
241  { return *adobe::max_element(time_set_m); }
242 
248  inline double accrued_average() const
249  { return empty() ? 0 : accrued_total() / size(); }
250 
256  inline double accrued_median() const
257  {
258  if (empty())
259  return 0;
260 
261  adobe::sort(time_set_m);
262 
263  return (size() % 2 == 1) ?
264  time_set_m[time_set_m.size() / 2] :
265  (time_set_m[time_set_m.size() / 2] +
266  time_set_m[time_set_m.size() / 2 - 1]) / 2;
267  }
268 
274  inline double accrued_total() const
275  { return adobe::accumulate(time_set_m, double(0)); }
276 
282  inline size_type size() const
283  { return time_set_m.size(); }
284 
290  inline bool empty() const
291  { return time_set_m.empty(); }
292 
300  inline void report(const char* decoration, std::ostream& s = std::cout)
301  {
302  double time(split());
303 
304  s << decoration << " took " << time << " milliseconds (" << time / 1e3 << " sec)" << std::endl;
305 
306  reset();
307  }
308 
309 private:
310 #ifndef ADOBE_NO_DOCUMENTATION
311  friend bool operator == (const timer_t& x, const timer_t& y);
312  friend bool operator < (const timer_t& x, const timer_t& y);
313 #endif
314 
315  value_type epoch_m;
316  value_type split_m;
317  mutable accumulator_type time_set_m;
318 #if ADOBE_PLATFORM_WIN
319  value_type frequency_m;
320 #endif
321 };
322 
323 /****************************************************************************************************/
324 
325 #ifndef ADOBE_NO_DOCUMENTATION
326 
327 inline bool operator == (const timer_t& /*x*/, const timer_t& /*y*/)
328 { return true; }
329 
330 /****************************************************************************************************/
331 
332 inline bool operator < (const timer_t& /*x*/, const timer_t& /*y*/)
333 { return false; }
334 
335 #endif
336 
337 /****************************************************************************************************/
338 
339 } // namespace adobe
340 
341 /****************************************************************************************************/
342 
343 #endif
344 
345 /****************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google