Advertisement Jump to content
Sign in to follow this  

accurate timing in linux?

This topic is 4892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I looked into how timing can be done in linux/unix and I ran into this function:
unsigned int getMilliSeconds() {
    struct timeb tb;
    return tb.time * 1000 + tb.millitm;

Is there a more accurate way of achiving this?

Share this post

Link to post
Share on other sites
According to the man page,


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.


Share this post

Link to post
Share on other sites
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);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!