All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Timer.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_TIMING_TIMER_H_
23 #define _BLAZE_UTIL_TIMING_TIMER_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <limits>
31 #include <blaze/util/Time.h>
32 #include <blaze/util/Types.h>
33 
34 
35 namespace blaze {
36 
37 namespace timing {
38 
39 //=================================================================================================
40 //
41 // CLASS DEFINITION
42 //
43 //=================================================================================================
44 
45 //*************************************************************************************************
89 template< typename TP > // Timing policy
90 class Timer
91 {
92  public:
93  //**Type definitions****************************************************************************
94  typedef TP TimingPolicy;
95  //**********************************************************************************************
96 
97  //**Constructor*********************************************************************************
100  explicit inline Timer();
102  //**********************************************************************************************
103 
104  //**Destructor**********************************************************************************
105  // No explicitly declared destructor.
106  //**********************************************************************************************
107 
108  //**Timing functions****************************************************************************
111  inline void start();
112  inline void end ();
113  inline void reset();
115  //**********************************************************************************************
116 
117  //**Get functions*******************************************************************************
120  inline size_t getCounter() const;
122  //**********************************************************************************************
123 
124  //**Time evaluation functions*******************************************************************
127  inline double total() const;
128  inline double average() const;
129  inline double min() const;
130  inline double max() const;
131  inline double last() const;
133  //**********************************************************************************************
134 
135  private:
136  size_t counter_;
137  double start_;
138  double end_;
139  double time_;
140  double min_;
141  double max_;
142  double last_;
143 };
144 //*************************************************************************************************
145 
146 
147 
148 
149 //=================================================================================================
150 //
151 // CONSTRUCTOR
152 //
153 //=================================================================================================
154 
155 //*************************************************************************************************
162 template< typename TP > // Timing policy
164  : counter_( 0 )
165  , start_ ( 0.0 )
166  , end_ ( 0.0 )
167  , time_ ( 0.0 )
168  , min_ ( std::numeric_limits<double>::max() )
169  , max_ ( 0.0 )
170  , last_ ( 0.0 )
171 {
172  // Starting the time measurement
173  start();
174 }
175 //*************************************************************************************************
176 
177 
178 
179 
180 //=================================================================================================
181 //
182 // TIMING FUNCTIONS
183 //
184 //=================================================================================================
185 
186 //*************************************************************************************************
193 template< typename TP > // Timing policy
194 inline void Timer<TP>::start()
195 {
196  // Starting the time measurement and calculating a time stamp
197  start_ = TimingPolicy::getTimestamp();
198 }
199 //*************************************************************************************************
200 
201 
202 //*************************************************************************************************
210 template< typename TP > // Timing policy
211 inline void Timer<TP>::end()
212 {
213  // Stopping the time measurement and calculating a time stamp
214  end_ = TimingPolicy::getTimestamp();
215 
216  // Increasing the counter
217  ++counter_;
218 
219  // Calculating the wallclock and CPU time
220  const double diff( end_ - start_ );
221 
222  // Average time measurement
223  time_ += diff;
224 
225  // Minimum time measurement
226  if( diff < min_ ) min_ = diff;
227 
228  // Maximum time measurement
229  if( diff > max_ ) max_ = diff;
230 
231  // Last time measurement
232  last_ = diff;
233 }
234 //*************************************************************************************************
235 
236 
237 //*************************************************************************************************
246 template< typename TP > // Timing policy
247 inline void Timer<TP>::reset()
248 {
249  counter_ = 0;
250  start_ = 0.0;
251  end_ = 0.0;
252  time_ = 0.0;
254  max_ = 0.0;
255  last_ = 0.0;
256 }
257 //*************************************************************************************************
258 
259 
260 
261 
262 //=================================================================================================
263 //
264 // GET FUNCTIONS
265 //
266 //=================================================================================================
267 
268 //*************************************************************************************************
273 template< typename TP > // Timing policy
274 inline size_t Timer<TP>::getCounter() const
275 {
276  return counter_;
277 }
278 //*************************************************************************************************
279 
280 
281 
282 
283 //=================================================================================================
284 //
285 // TIME EVALUATION FUNCTIONS
286 //
287 //=================================================================================================
288 
289 //*************************************************************************************************
294 template< typename TP > // Timing policy
295 inline double Timer<TP>::total() const
296 {
297  return time_;
298 }
299 //*************************************************************************************************
300 
301 
302 //*************************************************************************************************
307 template< typename TP > // Timing policy
308 inline double Timer<TP>::average() const
309 {
310  return time_ / counter_;
311 }
312 //*************************************************************************************************
313 
314 
315 //*************************************************************************************************
320 template< typename TP > // Timing policy
321 inline double Timer<TP>::min() const
322 {
323  return min_;
324 }
325 //*************************************************************************************************
326 
327 
328 //*************************************************************************************************
333 template< typename TP > // Timing policy
334 inline double Timer<TP>::max() const
335 {
336  return max_;
337 }
338 //*************************************************************************************************
339 
340 
341 //*************************************************************************************************
346 template< typename TP > // Timing policy
347 inline double Timer<TP>::last() const
348 {
349  return last_;
350 }
351 //*************************************************************************************************
352 
353 } // timing
354 
355 } // blaze
356 
357 #endif