49 #ifndef DOXY_IGNORE_THIS 52 #if defined(OM_CC_MIPS) 79 virtual ~TimerImpl() { ; }
81 virtual void reset(
void) = 0;
82 virtual void start(
void) = 0;
83 virtual void stop(
void) = 0;
84 virtual void cont(
void) = 0;
85 virtual double seconds(
void)
const = 0;
91 #if defined(WIN32) && (defined(_MSC_VER) || defined(__INTEL_COMPILER) || defined (__MINGW32__) ) 93 #ifndef DOXY_IGNORE_THIS 97 class TimerImplWin32 :
public TimerImpl
101 LARGE_INTEGER count_;
102 LARGE_INTEGER start_;
105 TimerImplWin32(
void);
106 ~TimerImplWin32(
void) { ; }
108 virtual void reset(
void);
109 virtual void start(
void);
110 virtual void stop(
void);
111 virtual void cont(
void);
112 virtual double seconds(
void)
const;
115 TimerImplWin32::TimerImplWin32(
void)
117 if (QueryPerformanceFrequency(&freq_)==FALSE)
118 throw std::runtime_error(
"Performance counter of of stock!");
122 void TimerImplWin32::reset(
void)
124 memset(&count_,0,
sizeof(count_));
125 memset(&start_,0,
sizeof(count_));
128 void TimerImplWin32::start(
void)
131 QueryPerformanceCounter(&start_);
134 void TimerImplWin32::stop(
void)
138 QueryPerformanceCounter(&stop_);
139 count_.QuadPart += stop_.QuadPart - start_.QuadPart;
142 void TimerImplWin32::cont(
void)
144 QueryPerformanceCounter(&start_);
147 double TimerImplWin32::seconds(
void)
const 149 return (
double)count_.QuadPart/(double)freq_.QuadPart;
153 #elif defined(__GNUC__) && defined(__POSIX__) 155 #ifndef DOXY_IGNORE_THIS 159 template <clock
id_t N>
160 class TimerImplPosix :
public TimerImpl
163 TimerImplPosix() : id_(N), seconds_(0.0)
169 virtual void reset(
void) { seconds_ = 0.0; }
171 virtual void start(
void) { seconds_ = 0.0; clock_gettime( id_, &start_ ); }
172 virtual void stop(
void)
175 clock_gettime( id_, &stop );
176 seconds_ += ( stop.tv_sec - start_.tv_sec );
177 seconds_ += ( (double(stop.tv_nsec-start_.tv_nsec)*1e-9) );
180 virtual void cont(
void) { clock_gettime( id_, &start_ ); }
182 virtual double seconds()
const {
return seconds_; }
191 #elif (defined(__GNUC__) || (defined(__INTEL_COMPILER) && !defined(WIN32))) && !defined(__MINGW32__) 193 # include <sys/time.h> 194 # include <sys/resource.h> 197 class TimerImplGToD:
public TimerImpl
200 TimerImplGToD() : seconds_(0.0)
206 virtual void reset(
void) { seconds_ = 0.0; }
207 virtual void start(
void) { seconds_ = 0.0; gettimeofday( &start_, &tz_ ); }
209 virtual void stop(
void)
211 gettimeofday( &stop_, &tz_ );
213 seconds_ += (double)(stop_.tv_sec - start_.tv_sec);
214 seconds_ += (double)(stop_.tv_usec- start_.tv_usec)*1e-6;
217 virtual void cont(
void) { gettimeofday( &start_, &tz_); }
219 virtual double seconds()
const {
return seconds_; }
223 struct timeval start_, stop_;
230 #else // ---------------------------------------- standard implementation ---- 234 static const unsigned long clockticks = CLOCKS_PER_SEC;
236 class TimerImplStd :
public TimerImpl
239 TimerImplStd() : freq_(clockticks),count_(0),start_(0) { reset(); }
240 ~TimerImplStd() { ; }
242 virtual void reset(
void) { count_ = 0; }
243 virtual void start(
void) { count_ = 0; start_ = clock(); }
244 virtual void stop(
void);
245 virtual void cont(
void) { start_ = clock(); }
246 virtual double seconds(
void)
const {
return (
double)count_/(double)freq_; }
250 unsigned long count_;
251 unsigned long start_;
254 void TimerImplStd::stop(
void)
256 unsigned long stop_ = clock();
257 count_ += stop_-start_;
266 #if defined(WIN32) && (defined(_MSC_VER) || defined(__INTEL_COMPILER) || defined(__MINGW32__)) 267 impl_ =
new TimerImplWin32;
268 #elif defined(__GNUC__) && defined(__POSIX__) 273 # if defined(CLOCK_REALTIME_HR) 274 impl_ =
new TimerImplPosix<CLOCK_REALTIME_HR>;
276 impl_ =
new TimerImplPosix<CLOCK_REALTIME>;
278 #elif (defined(__GNUC__) || (defined(__INTEL_COMPILER) && !defined(WIN32)) ) && !defined(__MINGW32__) 279 impl_ =
new TimerImplGToD;
281 impl_ =
new TimerImplStd;
318 return state_==Stopped ? impl_->seconds() : 0.0;
323 if (state_ == Running)
325 return as_string(impl_->seconds(),format);
337 bool negative =
false;
345 fraction = modf(seconds,&integer);
347 t = (
unsigned long)integer;
349 hour = short( t / 3600L );
351 min = short( t / 60L );
362 case Timer::Automatic:
364 ptr += sprintf(ptr,
"%02dh:",hour);
367 ptr += sprintf(ptr,
"%02dm:",min);
368 else if (ptr>
string && hour)
369 ptr += sprintf(ptr,
"00m:");
372 ptr += sprintf(ptr,
"%02d",sec);
373 else if (ptr>
string && min)
374 ptr += sprintf(ptr,
"00");
378 if (ptr >
string && sec)
380 sprintf(ptr,
".%.3fs",fraction);
389 else if ( fraction * 1e2 > 0.1)
390 sprintf(ptr,
"%.3fcs",fraction*1.e2);
391 else if ( fraction * 1e3 > 0.1)
392 sprintf(ptr,
"%.3fms",fraction*1.e3);
393 else if ( fraction * 1e6 > 0.1)
394 sprintf(ptr,
"%.1f\xb5s",fraction*1.e6);
395 else if ( fraction * 1e9 > 0.1)
396 sprintf(ptr,
"%.1fns",fraction*1.e9);
398 sprintf(ptr,
"%.1fps",fraction*1.e12);
406 ptr += sprintf(ptr,
"%02dh:%02dm:%02d",hour,min,sec);
407 sprintf(ptr,
".%.12fs",fraction);
417 sprintf(ptr,
"%02dh:%02dm:%02ds",hour,min,sec);
break;
419 ptr += sprintf(ptr,
"%02dm:%02d", min, sec);
420 sprintf(ptr,
".%.2fs",fraction);
429 case Timer::Seconds: sprintf(ptr,
"%.3fs",seconds);
break;
430 case Timer::HSeconds: sprintf(ptr,
"%.3fcs",seconds*1e2);
break;
431 case Timer::MSeconds: sprintf(ptr,
"%.3fms",seconds*1e3);
break;
432 case Timer::MicroSeconds: sprintf(ptr,
"%.1f\xb5s",seconds*1e6);
break;
433 case Timer::NanoSeconds: sprintf(ptr,
"%.1fns",seconds*1e9);
break;
442 #endif // DOXY_IGNORE_THIS
void reset(void)
Reset the timer.
void start(void)
Start measurement.
Format
Formatting options for member Timer::as_string()
double seconds(void) const
Returns measured time in seconds, if the timer is in state 'Stopped'.
std::string as_string(Format format=Automatic)
void stop(void)
Stop measurement.
void cont(void)
Continue measurement.