tags:

views:

29

answers:

3

On every frame of my application, I can call timeGetTime() to retrieve the current elapsed milliseconds, and subtract the value of timeGetTime() from the previous frame to get the time between the two frames. However, to get the frame rate of the application, I have to use this formula: fps=1000/delay(ms). So for instance if the delay was 16 milliseconds, then 1000/16=62.5 (stored in memory as 62). Then let's say the delay became 17 milliseconds, then 1000/17=58, and so on:

1000/10=100
1000/11=90
1000/12=83
1000/13=76
1000/14=71
1000/15=66
1000/16=62
1000/17=58
1000/18=55
1000/19=52
1000/20=50

As you can see for consecutive instances for the delay, there are pretty big gaps in the frame rates. So how do programs like FRAPS determine the frame rate of applications that are between these values (eg 51,53,54,56,57,etc)?

A: 

Not sure, but maybe you need better (high resolution) timer. Check QueryPerformanceTimer.

Marko
+1  A: 

Why would you do this on every frame? You'll find if you do it on every tenth frame, and then divide that value by 10, you'll easily get frame rates within the gaps you're seeing. You'll also probably find your frame rates are higher since you're doing less admin work within the loop :-)

In other words, something like (pseudo code):

chkpnt = 10
cntr = chkpnt
baseTime = now()
do lots of times:
    display next frame
    cntr--
    if cntr == 0:
        cntr = chkpnt
        newTime = now()
        display "framerate = " (newTime - baseTime) / chkpnt
        baseTime = newTime
paxdiablo
+1  A: 

In addition to @Marko's suggestion to use a better timer, the key trick for a smoothly varying and better approximate evaluation of the frame rate is to use a moving average -- don't consider only the very latest delay you've observed, consider the average of (say) the last five. You can compute the latter as a floating-point number, to get more possible values for the frame rate (which you can still round to the nearest integer of course).

For minimal computation, consider a "fifo queue" of the last 5 delays (pseudocode)...:

array = [16, 16, 16, 16, 16]   # initial estimate
totdelay = 80
while not Done:
    newest = latestDelay()
    oldest = array.pop(0)
    array.append(newest)
    totdelay += (newest - oldest)
    estimatedFramerate = 200 / totdelay
    ...
Alex Martelli