Calculating frames per second in a game. Question What's a good algorithm for calculating frames per second in a game? Popular Answer You need a smoothed average, the easiest way is to take the current answer the time to draw the last frame and combine it with the previous answer. Martin Beckett. This is what I have used in many games.
There are at least two ways to do it: The first is the one others have mentioned here before me. Then there is the uber cool way you might like to use some day: Let's say you have 'i' frames to consider. Peter Jankuliak. Enqueue Time. Email: [email protected]. Related Url. You''re on the right track! I just can''t figure out what''s causing my little demo to get such a low frame rate!
Sly Turn off vertical blanking. In Direct3D applications, try flipping without the wait flag. You will find that 60Hz is most likely the monitor refresh rate. Here is an interesting tid-bit. This topic is closed to new replies. Josheir Internship Possibility Games Career Development. Bonus points if your answer updates each frame and doesn't converge differently when the frame rate is increasing vs decreasing.
You need a smoothed average, the easiest way is to take the current answer the time to draw the last frame and combine it with the previous answer. By adjusting the 0. A larger fraction in favour of the old answer gives a slower smoother change, a large fraction in favour of the new answer gives a quicker changing value.
Obviously the two factors must add to one! But, as you point out, there's a lot of variation in the time it takes to render a single frame, and from a UI perspective updating the fps value at the frame rate is not usable at all unless the number is very stable.
For example, you could maintain a queue data structure which held the rendering times for each of the last 30, 60, , or what-have-you frames you could even design it so the limit was adjustable at run-time. To determine a decent fps approximation you can determine the average fps from all the rendering times in the queue:. When you finish rendering a new frame you enqueue a new rendering time and dequeue an old rendering time. Alternately, you could dequeue only when the total of the rendering times exceeded some preset value e.
You can maintain the "last fps value" and a last updated timestamp so you can trigger when to update the fps figure, if you so desire. Though with a moving average if you have consistent formatting, printing the "instantaneous average" fps on each frame would probably be ok. Another method would be to have a resetting counter. Maintain a precise millisecond timestamp, a frame counter, and an fps value. When you finish rendering a frame, increment the counter.
When the counter hits a pre-set limit e. Increment a counter every time you render a screen and clear that counter for some time interval over which you want to measure the frame-rate. The first is the one others have mentioned here before me. I think it's the simplest and preferred way.
You just to keep track of. Let's say you have 'i' frames to consider. I'll use this notation: f[0], f[1], Now the trick here is to modify the right side of formula 1 in such a way that it will contain the right side of formula 2 and substitute it for it's left side. So according to this formula my math deriving skill are a bit rusty though , to calculate the new fps you need to know the fps from the previous frame, the duration it took to render the last frame and the number of frames you've rendered.
This might be overkill for most people, that's why I hadn't posted it when I implemented it. But it's very robust and flexible. It stores a Queue with the last frame times, so it can accurately calculate an average FPS value much better than just taking the last frame into consideration. It also allows you to ignore one frame, if you are doing something that you know is going to artificially screw up that frame's time.
It also allows you to change the number of frames to store in the Queue as it runs, so you can test it out on the fly what is the best value for you. Good answers here. Just how you implement it is dependent on what you need it for. The fastest frame is at 7 ms or Hz. This shows that the work needs to be balanced out between frames, but is good on average, and even the worst case is probably acceptable. With this one you know that you are taking almost exactly the same amount of time for every frame, consistently.
It's a solid frame rate. After that, I feel the second most useful set of numbers is the processing time between frames. This number is going to fluctuate much more than the displayed framerate, but you are most interested in avoiding spikes.
It also gives you an idea of how much additional work you can do every frame. Finally, it is nice but not essential to have a one second average and a 5 second average, both updated every second. Throw in a bit of coloring on the numbers and render them in the corner. It is a very rough beginning to an in-game profiling system.
Later you'll want to add additional counters for processing time of AI, rendering, and everything else. It isn't that hard to do, but that's another post. Grantax If you just want the framerate, and you'd like it to update every second so it would actually be possible to read, would there be any downside to actually just counting the frames per second? This topic is closed to new replies. Josheir
0コメント