00001 #ifndef __TIMER_H__
00002 #define __TIMER_H__
00003
00004 #include <sys/time.h>
00005
00006
00007
00009
00010 class Timer
00011 {
00012 private:
00013
00015 off_t _usecs;
00016
00018 off_t _usecs_at_mark;
00019
00021 struct timeval _tstart;
00022
00024 struct timeval _tend;
00025
00027 struct timezone _tz;
00028
00030 bool _running;
00031
00032 public:
00033
00035 Timer() { reset(); }
00036
00038 void reset() { _usecs = _usecs_at_mark = 0; _running = false; }
00039
00041 void mark() { stop(); _usecs_at_mark = _usecs; cont(); }
00042
00044 inline void start()
00045 {
00046 _usecs = _usecs_at_mark = 0;
00047 gettimeofday(&_tstart, &_tz);
00048 _running = true;
00049 }
00050
00052 inline void cont()
00053 {
00054 if (_running == false)
00055 {
00056 gettimeofday(&_tstart, &_tz);
00057 _running = true;
00058 }
00059 }
00060
00062 inline void stop()
00063 {
00064 gettimeofday(&_tend, &_tz);
00065 if(_running){_usecs += (off_t)(1000000) * (off_t)(_tend.tv_sec -
00066 _tstart.tv_sec) + (off_t)(_tend.tv_usec -
00067 _tstart.tv_usec);}
00068 _running = false;
00069
00070 }
00071
00072
00073 inline void setUsecs(off_t usecs) { _usecs = usecs; }
00074 inline void setMsecs(off_t msecs) { _usecs = msecs * (off_t)(1000); }
00075 inline void setSecs(off_t secs) { _usecs = secs * (off_t)(1000000); }
00076
00078 off_t value() const { return _usecs; }
00079 off_t usecs() const { return _usecs; }
00080 off_t msecs() const { return _usecs/1000; }
00081 float secs() const { return _usecs/1000000.0; }
00082
00084 off_t usecs_since_mark() const { return _usecs - _usecs_at_mark; }
00085 };
00086
00087 #endif
00088