Jump to content
  • Advertisement
Sign in to follow this  
colinisinhere

accurate timing in linux?

This topic is 4823 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;
    ftime(&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
Advertisement
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

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 GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!