44 #ifndef DOXY_IGNORE_THIS 47 #if defined(OM_CC_MIPS) 74 virtual ~TimerImpl() { ; }
76 virtual void reset(
void) = 0;
77 virtual void start(
void) = 0;
78 virtual void stop(
void) = 0;
79 virtual void cont(
void) = 0;
80 virtual double seconds(
void)
const = 0;
86 #if defined(WIN32) && (defined(_MSC_VER) || defined(__INTEL_COMPILER) || defined (__MINGW32__) ) 88 #ifndef DOXY_IGNORE_THIS 92 class TimerImplWin32 :
public TimerImpl
100 TimerImplWin32(
void);
101 ~TimerImplWin32(
void) { ; }
103 virtual void reset(
void);
104 virtual void start(
void);
105 virtual void stop(
void);
106 virtual void cont(
void);
107 virtual double seconds(
void)
const;
110 TimerImplWin32::TimerImplWin32(
void)
112 if (QueryPerformanceFrequency(&freq_)==FALSE)
113 throw std::runtime_error(
"Performance counter of of stock!");
117 void TimerImplWin32::reset(
void)
119 memset(&count_,0,
sizeof(count_));
120 memset(&start_,0,
sizeof(count_));
123 void TimerImplWin32::start(
void)
126 QueryPerformanceCounter(&start_);
129 void TimerImplWin32::stop(
void)
133 QueryPerformanceCounter(&stop_);
134 count_.QuadPart += stop_.QuadPart - start_.QuadPart;
137 void TimerImplWin32::cont(
void)
139 QueryPerformanceCounter(&start_);
142 double TimerImplWin32::seconds(
void)
const 144 return (
double)count_.QuadPart/(double)freq_.QuadPart;
148 #elif defined(__GNUC__) && defined(__POSIX__) 150 #ifndef DOXY_IGNORE_THIS 154 template <clock
id_t N>
155 class TimerImplPosix :
public TimerImpl
158 TimerImplPosix() : id_(N), seconds_(0.0)
164 virtual void reset(
void) { seconds_ = 0.0; }
166 virtual void start(
void) { seconds_ = 0.0; clock_gettime( id_, &start_ ); }
167 virtual void stop(
void)
170 clock_gettime( id_, &stop );
171 seconds_ += ( stop.tv_sec - start_.tv_sec );
172 seconds_ += ( (double(stop.tv_nsec-start_.tv_nsec)*1e-9) );
175 virtual void cont(
void) { clock_gettime( id_, &start_ ); }
177 virtual double seconds()
const {
return seconds_; }
186 #elif (defined(__GNUC__) || (defined(__INTEL_COMPILER) && !defined(WIN32))) && !defined(__MINGW32__) 188 # include <sys/time.h> 189 # include <sys/resource.h> 192 class TimerImplGToD:
public TimerImpl
195 TimerImplGToD() : seconds_(0.0)
201 virtual void reset(
void) { seconds_ = 0.0; }
202 virtual void start(
void) { seconds_ = 0.0; gettimeofday( &start_, &tz_ ); }
204 virtual void stop(
void)
206 gettimeofday( &stop_, &tz_ );
208 seconds_ += (double)(stop_.tv_sec - start_.tv_sec);
209 seconds_ += (double)(stop_.tv_usec- start_.tv_usec)*1e-6;
212 virtual void cont(
void) { gettimeofday( &start_, &tz_); }
214 virtual double seconds()
const {
return seconds_; }
218 struct timeval start_, stop_;
225 #else // ---------------------------------------- standard implementation ---- 229 static const unsigned long clockticks = CLOCKS_PER_SEC;
231 class TimerImplStd :
public TimerImpl
234 TimerImplStd() : freq_(clockticks),count_(0),start_(0) { reset(); }
235 ~TimerImplStd() { ; }
237 virtual void reset(
void) { count_ = 0; }
238 virtual void start(
void) { count_ = 0; start_ = clock(); }
239 virtual void stop(
void);
240 virtual void cont(
void) { start_ = clock(); }
241 virtual double seconds(
void)
const {
return (
double)count_/(double)freq_; }
245 unsigned long count_;
246 unsigned long start_;
249 void TimerImplStd::stop(
void)
251 unsigned long stop_ = clock();
252 count_ += stop_-start_;
262 #if defined(WIN32) && (defined(_MSC_VER) || defined(__INTEL_COMPILER) || defined(__MINGW32__)) 263 impl_ =
new TimerImplWin32;
264 #elif defined(__GNUC__) && defined(__POSIX__) 269 # if defined(CLOCK_REALTIME_HR) 270 impl_ =
new TimerImplPosix<CLOCK_REALTIME_HR>;
272 impl_ =
new TimerImplPosix<CLOCK_REALTIME>;
274 #elif (defined(__GNUC__) || (defined(__INTEL_COMPILER) && !defined(WIN32)) ) && !defined(__MINGW32__) 275 impl_ =
new TimerImplGToD;
277 impl_ =
new TimerImplStd;
314 return state_==Stopped ? impl_->seconds() : 0.0;
319 if (state_ == Running)
321 return as_string(impl_->seconds(),format);
333 bool negative =
false;
341 fraction = modf(seconds,&integer);
343 t = (
unsigned long)integer;
345 hour = short( t / 3600L );
347 min = short( t / 60L );
358 case Timer::Automatic:
360 ptr += sprintf(ptr,
"%02dh:",hour);
363 ptr += sprintf(ptr,
"%02dm:",min);
364 else if (ptr>
string && hour)
365 ptr += sprintf(ptr,
"00m:");
368 ptr += sprintf(ptr,
"%02d",sec);
369 else if (ptr>
string && min)
370 ptr += sprintf(ptr,
"00");
374 if (ptr >
string && sec)
376 sprintf(ptr,
".%.3fs",fraction);
385 else if ( fraction * 1e2 > 0.1)
386 sprintf(ptr,
"%.3fcs",fraction*1.e2);
387 else if ( fraction * 1e3 > 0.1)
388 sprintf(ptr,
"%.3fms",fraction*1.e3);
389 else if ( fraction * 1e6 > 0.1)
390 sprintf(ptr,
"%.1f\xb5s",fraction*1.e6);
391 else if ( fraction * 1e9 > 0.1)
392 sprintf(ptr,
"%.1fns",fraction*1.e9);
394 sprintf(ptr,
"%.1fps",fraction*1.e12);
402 ptr += sprintf(ptr,
"%02dh:%02dm:%02d",hour,min,sec);
403 sprintf(ptr,
".%.12fs",fraction);
413 sprintf(ptr,
"%02dh:%02dm:%02ds",hour,min,sec);
break;
415 ptr += sprintf(ptr,
"%02dm:%02d", min, sec);
416 sprintf(ptr,
".%.2fs",fraction);
425 case Timer::Seconds: sprintf(ptr,
"%.3fs",seconds);
break;
426 case Timer::HSeconds: sprintf(ptr,
"%.3fcs",seconds*1e2);
break;
427 case Timer::MSeconds: sprintf(ptr,
"%.3fms",seconds*1e3);
break;
428 case Timer::MicroSeconds: sprintf(ptr,
"%.1f\xb5s",seconds*1e6);
break;
429 case Timer::NanoSeconds: sprintf(ptr,
"%.1fns",seconds*1e9);
break;
438 #endif // DOXY_IGNORE_THIS double seconds(void) const
Returns measured time in seconds, if the timer is in state 'Stopped'.
void reset(void)
Reset the timer.
std::string as_string(Format format=Automatic)
Format
Formatting options for member Timer::as_string()
void start(void)
Start measurement.
void cont(void)
Continue measurement.
void stop(void)
Stop measurement.