# Timing

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

## Recommended Posts

##### Share on other sites
I don't think this is a silly question and I would also very much like to know the answer. I have just spent weeks learning how to use QueryPerformanceCounter etc to create framerate-independant movement and I am a bit concerned that all my code is going to break when multi-core processors become more prevalent.

##### Share on other sites
I'm pretty sure QPC is the best you can get. I've never had trouble with it, although you scare me a bit because I have a x2 dual-core on the way. You scare me in a good way though, because I'd rather it break on my system than the end-users.

Physics intensive games don't worry about exact timing, they worry about exact physics. If you want to get exact physics, you use time integral methods to determine the exact time of each collision, then update the objects accordingly.

At the start of each frame, you calculate the time since the last update using QPC. Its only called once per frame, so you don't need to worry about any overhead. Also, you want to make sure and update every object with the same timestep, so its a win-win situation.

GetTickCount does work in most cases. The only reason not to use it is accuracy. I have a networking program that measure ping using GetTickCount. The problem is my pings are either 0, 10, 20... and nothing in between.

So, in short, just use QPC (especially if you already got it working on your dual-core). And don't worry about the speed as it should only be called once per frame.

##### Share on other sites
I've heard that QueryPerformanceCounter returns erroneous information on multi-processor or multi-core processor systems, presumabley because each processor has its own performance counter.

##### Share on other sites

Quote:
 You scare me in a good way though, because I'd rather it break on my system than the end-users.

I feel the same way. The break didn't occur until I switched from softwarevertexprocessing to hardware, strangely.. Not sure how that would have anything to do with QPC.

Here's my loop for the sake of it:

while (app.Created){  QueryPerformanceFrequency(ref freq);  QueryPerformanceCounter(ref start);  app.Update();  app.Render();  Application.DoEvents();  QueryPerformanceCounter(ref end);  app.fr = (float)((double)(end - start) / freq);  app.accumulator += app.fr;}

Update checks to see if the accumulator has gone past a timeslice (1/60 in my case) and if it has, processes all necessary updates and decriments the accumulator by the framerate, etc. etc.

Anything I should change? I wasn't sure if I should continually call QPF to check for CPUs that throttle or not.

##### Share on other sites
Quote:
 Original post by DaveInsurgent*** Source Snippet Removed ***

To me you seem to have both a bit strange structure and a bit strange invoktion of QPC, check out PInvoke.net for the prefered way.

I would write that loop like this:
long fequency;QueryPerformanceFrequency(out freqeuncy);float freq = (float)frequency;long oldTime;long newTime;float dt;while (app.Created){    // Calculate the delta time.    oldTime = newTime;        QueryPerformanceCounter(out newTime);    dt = (newTime - oldTime) / freq;    // Now do delta time dependant stuff    app.Update(dt); // Pass the delta time    app.Render();    // I think this looks a bit dirty, but I'll just leave it here :)    app.accumulator += app.fr;    Application.DoEvents();}

[Edited by - Enselic on April 21, 2006 7:37:20 AM]

##### Share on other sites
First, frequency scaling (Like AMD's Cool & Quiet) should *not* cause problems with QPC. At least according to Microsoft, that is "guaranteed" to work.

Multi-core systems are trickier though. There's an article about how to manage it somewhere on MSDN... Don't have the link though.
But as I remember, they basically suggest comparing the value returned from QPC to the last result, to see if it's reasonably close to the last result. If it starts telling you that -3 seconds has elapsed since last frame, you know there's something wrong... [wink]

But I guess a good start would be to just decide what kind of precision you need. timeGetTime can do 1ms precision, and works no matter what.

QPC is more accurate, but you start getting problems with multicore systems.
So, is 1ms enough? Then you don't even have to deal with this problem.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 9
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632147
• Total Posts
3004445

×