Timing in linux

Started by
3 comments, last by DIB 18 years, 4 months ago
Here is the meat of my timer class:

#include "../core/Timer.h"

Timer::Timer() {	
	m_fStartTime = getMilliSeconds();
}

Timer::~Timer() {	
}

void Timer::start() {
	m_fStartTime = getMilliSeconds();
}

f64  Timer::getInterval() {
	return (getMilliSeconds()-m_fStartTime);
}

f64 getMilliSeconds() {
	#ifdef LINUX_X86
		struct timeval tm;
		struct timezone tz;
		gettimeofday(&tm, &tz);
	
		return (((double)(tm.tv_sec) + (double)(tm.tv_usec) * 0.000001))*1000;
	#endif
}

I used this time to rotate a cube, to test how accurate the timing is. But it is not accurate at all. It is very inconsistant, and jerky. Really nast on the eyes. Any ideas why it is like this? (BTW, f64 is double)
Advertisement
/*  Returns the time in milliseconds */unsigned long int getTicks(timeval &start){  timeval now;  unsigned long int ticks;  gettimeofday(&now, NULL);  ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;  return ticks;}int main(){  // timer data  static timeval start;  // timer vars  unsigned long int beg, end;  // get current time   gettimeofday(&start, NULL);  // time  beg = getTicks(start);  doBenchMark1();  end = getTicks(start);   cout << "BenchMark 1 took " << end-beg << " milliseconds or " <<           (end-beg)/1000.0 << " seconds."  return 0;}
This timer does the same thing, inaccurate, and spuratic.

Any ideas?
Actually, I checked back, compiled in release mode, and close a couple of x apps, and it was much better.

But it still isn't as good as I want/need it to be. Does anyone have a snippet of how Quake 3 does it? Since now it is GPL. I'd rather not look it up myself if someone has it handy, but I will if I have to.
gettimeofday is about as good as it gets for getting the time under Linux. Keep in mind your scheduler...you may notice 20ms of variation from frame to frame, depending on your system. If you are rotating your cube at, say, 60hz, then this should be more than accurate enough to smoothly rotate. If not, then you might want to look elsewhere in your code. I don't suppose you have a profiler?

This topic is closed to new replies.

Advertisement