vtf-logo

Timer.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00008 #if !defined(__ads_Timer_h__)
00009 #define __ads_Timer_h__
00010 
00011 #include "../defs.h"
00012 
00013 #include <ctime>
00014 #include <cassert>
00015 #include <limits>
00016 
00017 BEGIN_NAMESPACE_ADS
00018 
00020 class Timer {
00021 public:
00022 
00024   typedef double Number;
00025 
00026 private:
00027 
00028   clock_t _start;
00029 
00030 public:
00031 
00033   Timer() :
00034     // Initialize with an invalid time.
00035     _start(std::numeric_limits<clock_t>::max())
00036   {}
00037 
00039   Timer(const Timer& x) :
00040     _start(x._start)
00041   {}
00042 
00044   const Timer& 
00045   operator=(const Timer& x) {
00046     // Avoid assignment to self
00047     if (&x != this) {
00048       _start = x._start;
00049     }
00050     // Return *this so assignments can chain
00051     return *this;
00052   }
00053 
00055   ~Timer()
00056   {}
00057 
00059   void 
00060   tic() {
00061     _start = std::clock();
00062   }
00063 
00065   Number 
00066   toc() {
00067     clock_t end = std::clock();
00068     // Make sure tic() has been called and the call worked.
00069     assert(_start != clock_t(- 1));
00070     // Make sure the end time worked.
00071     assert(end != clock_t(-1));
00072     // Return the elapsed time in seconds.
00073     return Number(end - _start) / CLOCKS_PER_SEC;
00074   }
00075 
00076 };
00077 
00078 END_NAMESPACE_ADS
00079 
00080 #endif

Generated on Fri Aug 24 12:55:33 2007 for Algorithms and Data Structures Package by  doxygen 1.4.7