src/util/Timer.h

Go to the documentation of this file.
00001 
00010 #ifndef TIMER_H
00011 #define TIMER_H
00012 
00013 #include <sys/time.h>
00014 #include <unistd.h>
00015 
00016 namespace green
00017 {
00023         class Timer
00024         {
00025                 protected:
00026                         bool mTiming;
00027                         double mStartTime;
00028                         double mStopTime;
00029                         
00030                 public:                         
00034                         virtual ~Timer() {}
00035                         
00048                         inline
00049                         static double GetCurrentTime()
00050                         {
00051                                 struct timeval tv;
00052                                 // FIXME: check return value of gettimeofday. man page doesn't say what it should be
00053                                 gettimeofday(&tv, NULL);
00054                                 // tv_usec is microseconds, which is 1 millionth of a second
00055                                 // FIXME: gettimeofday is returning not 1/10000 of a second, not microsecond?
00056                                 return (double(tv.tv_sec) + (double(tv.tv_usec) * 0.000001));
00057                         }
00058 
00063                         Timer() : 
00064                                         mTiming(false),
00065                                         mStartTime(Timer::GetCurrentTime()),
00066                                         mStopTime(0.0)
00067                         {}
00068                                                                                                                  
00073                         virtual inline
00074                         void StartTiming()
00075                         {
00076                                 mStartTime = GetCurrentTime();
00077                                 mTiming = true;
00078                         }               
00079                         
00084                         virtual inline
00085                         void StopTiming()
00086                         {
00087                                 mTiming = false;
00088                                 mStopTime = GetCurrentTime();
00089                         }
00090                         
00095                         virtual inline
00096                         bool IsTiming() const {
00097                                 return mTiming;
00098                         }
00099                         
00105                         virtual inline
00106                         double GetStartTime() const
00107                         {
00108                                 return mStartTime;
00109                         }
00110                         
00116                         virtual inline
00117                         double GetStopTime() const
00118                         {
00119                                 if(mTiming)
00120                                         return 0.0;
00121                                 else
00122                                         return mStopTime;
00123                         }
00124                         
00131                         virtual inline
00132                         double GetTimeDelta() const
00133                         {
00134                                 if(mTiming)
00135                                 {
00136                                         double currentDelta = GetCurrentTime() - mStartTime;
00137                                         return (currentDelta > 0.0 ? currentDelta : 0.0);
00138                                 }
00139                                 else
00140                                 {
00141                                         double oldDelta = mStopTime - mStartTime;
00142                                         return (oldDelta > 0.0 ? oldDelta : 0.0);
00143                                 }
00144                         }
00145         };
00146 }
00147 
00148 #endif

Generated on Fri Apr 27 10:27:38 2007 for Green Engine by  doxygen 1.5.1