Sign in to follow this  
Paprica

Why is using clock() considered bad for graphical animations?

Recommended Posts

Hello.
Why do programmers say that clock() is bad and that I should use functions like QueryPerformanceCounter() instead?
clock()'s performance looks pretty reliable to me...

Share this post


Link to post
Share on other sites
Clock ticks (under Windows, anyway) are typically reliable to roughly 15ms. This is barely enough to guarantee stable 60Hz framerate, and is definitely not good enough for any higher framerate. On the other hand, QueryPerformanceCounter (and the Unix/Linux flavors) are accurate to a hundred nanoseconds or so, and they aren't much more expensive than using clock ticks. Why would you [i]not[/i] use superior technology? Edited by Bacterius

Share this post


Link to post
Share on other sites
[list]
[*]POSIX requires that CLOCKS_PER_SEC equals 1000000 independent of the actual resolution. Note that the time can wrap around. On a 32-bit system where CLOCKS_PER_SEC equals 1000000 this function will return the same value approximately every 72 minutes.[sup][[url="http://linux.die.net/man/3/clock"]1[/url]][/sup] (Windows isn't one of these POSIX systems, but this is something to be very aware of)
[*]The clock() function returns an approximation of [b]processor time [/b]used by the program.[sup][[url="http://linux.die.net/man/3/clock"]2[/url]][[url="http://en.cppreference.com/w/cpp/chrono/c/clock"]3[/url]][/sup] (emphasis mine). That is, it doesn't return the amount of actual, real time (though in VC++ I think it does). [url="http://stackoverflow.com/questions/6114392/clocks-per-sec-not-matching-results-from-stdclock"]It can cause issues if you use it wrong[/url].
[*]The resolution of clock() is implementation defined, and there's a good chance that at some point in time, its (usually) poor resolution isn't good enough for what you're doing (like Bacterius mentioned).
[/list]

Share this post


Link to post
Share on other sites
as long as you don't require high precession, and use correct time-stepping code, then i don't see anything really wrong with clock, implementations may differ, but that's generally what the define of CLOCKS_PER_SEC is for, still, i use QueryPerformanceCounter because it's as high resolution of a timer you can get.

still i recommend giving this a read:
[url="http://gafferongames.com/game-physics/fix-your-timestep/"]http://gafferongames.com/game-physics/fix-your-timestep/[/url]

for learning about generally proper implementations of timers.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this