Advertisement

accurate timing in linux?

Started by August 29, 2005 02:23 AM
3 comments, last by Generic Guest 19 years, 3 months ago
I looked into how timing can be done in linux/unix and I ran into this function:

unsigned int getMilliSeconds() {
    struct timeb tb;
    ftime(&tb);
    return tb.time * 1000 + tb.millitm;
}

Is there a more accurate way of achiving this?
According to the man page,

Quote:
This function is obsolete. Don't use it. If the time in seconds suffices, time(2) can be used; gettimeofday(2) gives microseconds; clock_gettime(3) gives nanoseconds but is not yet widely available.


gettimeofday is normally the preferred function. It's accurate to 1us on most systems, which is typically enough for games.

Mark
Advertisement
like markr said, gettimeofday is the function for you.

like so:

double getAccurateTimeInSeconds(){  struct timeval tm;  struct timezone tz;  gettimeofday(&tm, &tz);  return (double)(tm.tv_sec) + ((double)(tm.tv_usec) * 0.000001);}

Thank you for the replies! But I had trouble compiling that function, what headers must be included?

Thanks :)
As man recommends us :D

#include <sys/time.h>

This topic is closed to new replies.

Advertisement