00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <sys/time.h>
00023 #include <Basics.H>
00024 #include "Timer.H"
00025
00026 void Timer::T::Start() throw ()
00027 {
00028 assert(!this->running);
00029 this->running = true;
00030 struct timezone tzp;
00031 int code = gettimeofday(&(this->startTime), &tzp); assert(code == 0);
00032 }
00033
00034 Timer::MicroSecs Timer::T::Stop() throw ()
00035 {
00036 assert(this->running);
00037 this->running = false;
00038 struct timeval endTime;
00039 struct timezone tzp;
00040 int code = gettimeofday(&endTime, &tzp); assert(code == 0);
00041 MicroSecs delta = 1000000UL * (endTime.tv_sec - this->startTime.tv_sec);
00042 delta += (endTime.tv_usec - this->startTime.tv_usec);
00043 this->elapsed += delta;
00044 return delta;
00045 }
00046
00047 Timer::MicroSecs Timer::T::Reset() throw ()
00048 {
00049 assert(!this->running);
00050 MicroSecs res = this->elapsed;
00051 this->elapsed = 0UL;
00052 return res;
00053 }
00054
00055 Timer::MicroSecs Timer::Grain;
00056
00057 static void Timer_InitGrain() throw ()
00058
00059 {
00060 const int Incr = 100;
00061 int cnt = 0;
00062 Timer::T timer;
00063 while (1) {
00064 timer.Start();
00065 for (int j = 0; j < cnt; j++) {
00066 int k = j/3; k *= 3;
00067 }
00068 Timer::MicroSecs t = timer.Stop();
00069 if (t != 0UL) {
00070 Timer::Grain = t;
00071 break;
00072 }
00073 cnt += Incr;
00074 }
00075 }
00076
00077 class TimerInit {
00078 public:
00079 TimerInit() throw () { Timer_InitGrain(); }
00080 };
00081 static TimerInit t;