Click here to Skip to main content
13,597,195 members
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 27 Jul 2007
Licenced CPOL

Framerate Counter

, 27 Jul 2007
Rate this:
Please Sign up or sign in to vote.
A minimalist framerate counter.

Screenshot - framerate.jpg


This is a quick, functional frame-rate counter for your graphics app which draws and draws (as typical games do). It could be updated for use in other apps, such as keeping track of bytes downloaded of a file. The rate could then be used to estimate a time. However, this is provided with the intent of using it as a counter for your graphical performance.

Using the code

This may look familiar, but it's not. I've seen two types of frame-rate counters. This is one, the other is a function which returns the callCount, and the Paint routine checks: If frameRate() > 0 Then and promptly updates a local value. This one differs in that it does this for you, and changes the value of a variable (herein called currentFrameRate). The value is changed once per second, and is set to the number of times your Paint loop was executed that second. You only need to use this value as needed, such as drawing it onto your surface, and GDI+ based instructions are provided for this. It's by no means decimal-point-accurate, and I would allow a variation of about 1. However, if your frame-rate gets below 10, then I would be concerned about the user experience regardless of the code's potential inaccuracy.

Creative people may even be able to adapt this to other uses, such as keeping track of a process' performance (objects dealt with per second). You could even change the time to allow you a call count per minute. However, it is configured for seconds, and if you change the time scale, be wary of the callCount becoming too high.

To actually use the code:

  1. Insert the following code to the source:
  2. ' Framerate Counter
    Private Sub advanceFrameRate()
        Static ptlu As Double ' Time of last framerate update.
        ' Show me hardware that can do 32k F/sec...
        Static callCount As Integer
        ' Increment the callCounter
        callCount += 1
        ' Change 1000 if an alternate time value is desired.
        If (Environment.TickCount - ptlu) >= 1000 Then
            currentFrameRate = callCount
            ' Reset the callCount, AFTER updating the value.
            callCount = 0
            ' Reset the timeUpdated
            ptlu = Environment.TickCount
        End If
    End Sub
  3. Add the following to your Declarations section, and ensure the function will have access to it (e.g., keep them in the same class):
  4. Private currentFrameRate As Integer
  5. In your drawing routine (e.g., myForm_Paint), add the following lines:
  6. ' The Call statement is not needed, but makes it obvious.
    Call advanceFrameRate()
    e.Graphics.DrawString(paintingFrameRate & " F/sec", & _
        framerateFont, Brushes.Black, Xpos, Ypos)

    Note: the above line uses GDI+ and assumes you are in the Me.Paint handler. Feel free to employ DirectX drawing, or even use a MessageBox to report this to the user. framerateFont is a System.Drawing.Font of your choosing. Also, the brush colour can change, especially if your background is black. I tend to use:

    Private framerateFont As Font = _
            New Font("Verdana", 12, FontStyle.Regular, GraphicsUnit.Pixel)

    Note: This line requires Imports System.Drawing in the declarations. Xpos and Ypos represent wherever you wish to put the code on your form, and can be replaced with a System.Drawing.Point if need be.

I'm not totally fussed about whether you put my name on the code or not. Although, I would quite enjoy downloading an app which had "Contains code from a Ninja" on the download page.


This is the original version which I submitted.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Scotland Scotland
Hobbyist developer from the UK.
I just make stuff for the benefit of making stuff.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.180621.3 | Last Updated 27 Jul 2007
Article Copyright 2007 by codemunkeh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid