Jump to content
  • Advertisement
Sign in to follow this  
EternityZA

queryPerformanceCounter frequently jumps to far.

This topic is 3266 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

Hey! i use the queryPerformanceCounter/queryPerformanceFrequency to ensure that my logic steps 100 times per second. But every couple of seconds it jumps far into the future causing my logic to step 50 odd times at once to catch up which basicly sends my simulation 0.5 sec into the future in an instant. This is quite anoying but "beareble" on my high end desktop pc but on my laptop its much worse. Ive read posts where similar problems were described but i could not find a clear solution. Is queryPerformanceCounter/queryPerformanceFrequency realy the best way to get acurate time? How does commercial quality game engines ussualy do it? Thnx in Advance!

Share this post


Link to post
Share on other sites
Advertisement
That can be a problem with QueryPerformanceCounter and is usually due to hardware (or driver, actually) bugs. It seems odd that you have the same problem with two different computers, though.

Commercial games use a combination of techniques to get accurate timing. Sometimes they simply use timeGetTime (and timeBeginPeriod(1)) instead, sometimes they do something more complicated.

The simplest solution would be if the frame time is greater than a certain amount (e.g. 1/30s) to simply return the hard-coded value. That is,
if (frameTime > (1/30.0f)
frameTime = (1/30.0f);
A more elaborate solution might be to take the average frame time over the last n frames, and if the current frame time is too far from that average, to return the average instead. That'll be slightly more accurate, though slightly more complex.

Share this post


Link to post
Share on other sites
No, it is not the best way, except if you target Vista and later on the most modern processors only.

In fact, QPC is not only not the best way, but it is probably the worst way to measure time. On less-than-last-generation CPUs and/or less-than-Vista, QPC uses the RDTSC instruction (except on Windows 95/98 where it uses some other weird stuff). So, QPC has all the problems of RDTSC (most notably, different cores being out of sync, a.k.a "jumps", and Speedstep slowdowns), plus it has OS call overhead, plus you can't even rely on it using RDTSC.

timeGetTime() works reasonably well, although with lower resolution. At least, it's reliable, though. If you need the higher resolution, you should implement a RDTSC counter yourself and make sure that this thread runs on a known CPU (by setting affinity). I'm using a construct that blocks the RDTSC reader thread in a semaphore. For getting a time, you post it. Sounds horrible, but it works surprisingly well.
Also, you'll need to make sure your program uses enough CPU power to prevent power saving features from clocking down the CPU, but this is usually not a problem for a game. :-)

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!