A Simple GLFW FPS Counter

Update – September 2013: Need something that enforces a given frame rate rather than just reports the current frame rate? Try this: http://r3dux.org/2012/12/fpsmanager-a-c-helper-class-for-framerate-independent-movement/

I’m fed up of solving the same problem over and over again, so the next time I need some FPS measurements, I’m going to use this…

The Include Requirements

The Function

Usage Examples

Call any of these in your main loop…


I think that’s pretty usable and clean – if you’ve got any suggestions I’d really be interested in hearing them – I simply don’t want to re-implement a FPS counter in C++ ever again.

4 thoughts on “A Simple GLFW FPS Counter”

  1. I might be wrong but I think your counter misses a frame, fpsFrameCount should be reseted to 1 and not 0, otherwise you miss counting one frame when is time to return a new FPS value. I tried running your counter with no low time interval cap and it kept returning 0 fps

    1. Hmm, I can see what you’re getting at – and you’re right, resetting the fpsFrameCount to 1 does seem to provide FPS values, while leaving it at 0 returns a FPS value of zero.

      So picking it apart… doing away with the interval entirely should mean that the (now non-existent) interval elapses every time, so I’d expect the value fpsFrameCount to still be 0 because it never goes to the else condition to indicate “the interval hasn’t elapsed yet, so add 1 to the frame count”. When we divide 0 by the frame interval, we get 0.

      When we reset to a value of 1, still with no interval, we’re going to be re-calculating the FPS value with 1 frame divided by the duration of that frame – so we get the correct FPS value.

      But, my thinking when writing it was “Count how many frames have been drawn in a given interval” – once that interval has expired, we’ve drawn 0 frames since the interval expired – so I still like resetting to 0 as that feels like the factually correct thing to do. But something has to change, and I think that something is to move the frame count adder to the top of the function and let it always run (i.e. if the calcFPS() function runs, the fpsFrameCount gets incremented).

      How about this:

      Change summary:
      – Because we’re adding to the frame count early, I’ve also set the initial frameCount value to be -1.0 so that it bumps to 1.0 on first run and the very first FPS reading is zero. We can’t get an accurate value on a first run with a zero interval because we don’t have a start time to measure the frame duration, so a very first FPS value of 0 is preferable than some large value about the duration between consequtive calls to glfwGetTime(),
      – The frame count gets reset to zero at interval change (as before – but this is correct!),
      – It works properly now with a zero interval (which I’ve made allowable),
      – I’ve renamed the variables to make things more sensible, and finally
      – It should run a little bit faster as I’ve removed the cast to double on frameCount and got rid of a divide by re-using a duration value.

      Cheers, and thanks for your feedback!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.