00001 #ifndef TIMER_H
00002 #define TIMER_H
00003
00004 #include <sys/time.h>
00005 #include <stdlib.h>
00006
00007
00008 class Timer
00009 {
00010 public:
00011 Timer()
00012 {
00013 startCount.tv_sec = startCount.tv_usec = 0;
00014 endCount.tv_sec = endCount.tv_usec = 0;
00015 stopped = 0;
00016 startTimeInMicroSec = 0;
00017 endTimeInMicroSec = 0;
00018 }
00019
00020 ~Timer()
00021 {
00022 }
00023
00024 void start()
00025 {
00026 stopped = 0;
00027 gettimeofday(&startCount, NULL);
00028 }
00029
00030 void stop()
00031 {
00032 stopped = 1;
00033 gettimeofday(&endCount, NULL);
00034 }
00035
00036 double getElapsedTime()
00037 {
00038 return this->getElapsedTimeInSec();
00039 }
00040
00041 double getElapsedTimeInSec()
00042 {
00043 return this->getElapsedTimeInMicroSec() * 0.000001;
00044 }
00045
00046 double getElapsedTimeInMilliSec()
00047 {
00048 return this->getElapsedTimeInMicroSec() * 0.001;
00049 }
00050
00051 double getElapsedTimeInMicroSec()
00052 {
00053 if(!stopped)
00054 {
00055 gettimeofday(&endCount, NULL);
00056 }
00057
00058 startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
00059 endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
00060
00061 return endTimeInMicroSec - startTimeInMicroSec;
00062 }
00063
00064 private:
00065 double startTimeInMicroSec;
00066 double endTimeInMicroSec;
00067 int stopped;
00068 timeval startCount;
00069 timeval endCount;
00070
00071 };
00072
00073 #endif // TIMER_H