trying to calculate delta time each frame and getting weird results?

Started by
3 comments, last by ville-v 15 years, 4 months ago
I'm trying to calculate delta time each frame, but the results seem odd, especially compared to the amount of frames I'm getting? Useing timeGetTime() for simplicity sake. I get the time at the beginning of a frame and save it, on the next frame I get the new time and subtract the old time from it to get DELTA. then I do a calculation like so.

unsinged int DELTA;
float decimalDELTA;

decimalDELTA = int( DELTA % 1000 );
decimalDELTA *= 0.001;

I'm doing this to turn the delta into a decimal value to make other parts of my code easier to adjust. The weird part and I'm not sure if it's getting the right value, the decimalDELTA averages 0.081, but the frame rates hover around 524. If I'm correct using the frames per second to get the delta it'd be 1/524 or 0.0019. Technically the 0.0009 would be out of range for timeGetTime(), but still I should be averageing 0.001 for the delta right?
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Advertisement
timeGetTime() has around 5-10 ms of accuracy. Which means, you'd never get 0.0019 (1.9 ms). Try using QueryPerformanceCounter and QueryPerformanceFrequency.
If for some reason you don't want to use the Performance Counters, you can look at this.

http://msdn.microsoft.com/en-us/library/ms713418(VS.85).aspx

Notably this section:

"Windows NT/2000: The default precision of the timeGetTime function can be five milliseconds or more, depending on the machine. You can use the timeBeginPeriod and timeEndPeriod functions to increase the precision of timeGetTime. If you do so, the minimum difference between successive values returned by timeGetTime can be as large as the minimum period value set using timeBeginPeriod and timeEndPeriod. Use the QueryPerformanceCounter and QueryPerformanceFrequency functions to measure short time intervals at a high resolution,"

If you read into those, you can see that using the timeBeginPeriod, and timeEndPeriod, you can set the precision to a 1ms, but it might degrade the performance of the machine (since it's a system wide setting (note, that this is set to the lowest resolution any programmers, as for, meaning if you set yours to 1, everything will be at 1, and can't make it go higher, until timeEndPeriod is called, at least), processes and such might be switching more often, causing a degrade).

But, I'd just use the Performance Counters ;-)

EDIT: I'm not going to edit it, but see if you can decipher through that horrible grammar in that second paragraph. XD
"I'd rather know one thing, no matter how ordinary, than discourse endlessly on great issues." -- Galileo
Why are you doing a modulus of the delta?
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
For what are you needing such a framerate? My LCD monitor can draw 50 frames per second.

You have now:

loop(){    do_something();    count_time();}


Change it into:

loop(){    repeat 10 times{        do_something();        // If you need time here, use your counter-variable and latest time to calculate it    }    count_time();}

This topic is closed to new replies.

Advertisement