AS3 – Calculating true Frames Per Second

I strongly recommend you look at this new version of this class.

When coding for Flash based games it’s important to know how fast your project is running [even if you're not coding games it's good to know whether or not your code is slogging down the player]. Adobe’s Stage class provides a parameter called “frameRate,” which allows you to dynamically get and set the player’s frame rate. Well, not exactly. It allows you to get and set the player’s TARGET frame rate, the fact is if the player can’t perform your code at that frame rate, it won’t! So how can you find out what the real frame rate is?

I wrote a simple class for this task to use in my project development. It’s super simple. My class, fpsBox, adds an eventListener to the stage [if you pass the stage as an argument when instantiating the class] or to itself, that triggers every frame and increments a value. A timer is started simultaneously with a repeat cycle of one second. Every second the number of counts counted each frame are displayed in the fpsBox and then averaged in an array of instantaneous values. Don’t worry, the array never gets past two values, the current average and the instantaneous FPS. Here’s some usage:

import com.efnx.fps.fpsBox;

var fps:fpsBox = new fpsBox(stage);
     addChild(fps);

That’s it!

What it looks like:

Link to SWF

Source Code
fpsBox Class Source Code

Tags: , , , , ,

15 Responses to “AS3 – Calculating true Frames Per Second”

  1. Brandon Ward Says:

    Very cool! Thanks!

    Just FYI – in the usage, it should be

    addChild(fps);
    not
    addChild(fpsBox);

  2. Ah, thanks Brandon, glad you caught that!

  3. Alex Ionescu Says:

    Thanks!

  4. Thank’s a lot, very helpfull !

  5. thanks, that’s great. i tweaked it for as2 and no classes… outputs to variable ‘op’

    timerInt = setInterval(tick, 1000);
    this.onEnterFrame = function() { frames++; }
    function tick(){
    averageArray = [];
    averageArray.push(frames);
    if(averageArray.length == 4) {
    for(i = 1; i < averageArray.length; i++) {
    averageArray[0] += averageArray[i];
    }
    averageArray.splice(1, averageArray.length – 1);
    averageArray[0] /= 4;
    }
    op = frames + ” / ” + Math.round(averageArray[0]);
    frames = 0;
    }

  6. Nice! Very helpfull class for testing! Thanx!

  7. One of the best FPS I’ve found so far; thanks for sharing!

  8. Sho-to-ka-ka-a-like:)

  9. I have been playing with this a lot recently and few points I’ve discovered:

    1) Flash has some kind of restriction of 15-16milliseconds between calling functions like onEnterFrame or if you do setInterval(tick,1); . Even if you set the clip fps to 120 the onEnterFrame is really only called at about 64 times per second. However the Flash “cheats” this as it queues the onEnterFrame calls and calls the function multiple times on exact same millisecond.

    2) The FPS set for the clip affects greatly how consistently and often flash calls the onEnterFrame function. If you set the clip FPS to 120, the flash will execute the code pretty consistently between 15-16 ms. If you set the clip frame rate to 60PFS you will have your code executed between 15-32 milliseconds. Drop FPS more and it gets to 15-47ms. This makes it very difficult to make for example smooth right to left news scroll with flash. You newer know how consistently your code is executed.

    3) the second point also applies to setInterval(tick,1000) function. If you wanna be really accurate you should not use it because depending to your frame rate, it gets called between 1000-1047 milliseconds..

    After having said this… My testing code for AS2 looks as follows:
    ———————–

    testFPS = _root.createTextField(“testFPS”, _root.getNextHighestDepth(), 400, 50, 300, 30);
    var FPSTestDate:Date = new Date();
    var myPrevTime:Number = FPSTestDate.getTime();
    var myPrevDelta:Number = myPrevTime;
    var myTimeNow:Number = myPrevTime;
    var myFrameCounter:Number = 0;

    function onEnterFrame() {
    FPSTestDate = new Date();
    myTimeNow = FPSTestDate.getTime();
    // We will not count it towards FPS if there are simultaneous calls to this function
    // Only counted if the calls have atleast 1ms apart
    if (myPrevDelta = myPrevTime+1000) {
    testFPS.text = “onEnterFrame FPS:”+myFrameCounter;
    myPrevTime = myTimeNow – (myTimeNow-myPrevTime-1000);
    myFrameCounter = 0;
    }
    }

    ———————–

    Has anyone else come across of this “cheating” and anyway to go around it? Or is it just impossible to do ultra smooth scrolls with flash? If I set my clip to 120FPS I really would like to have my code called between consistent 8ms rather than “15-16ms with cheated simultaneous double calls”. Or if I set it to 60FPS Id like to get consistent ~16ms rather than 15-36ms.

    Anyone with some insights to Flash’s “soul” and workings?

  10. Dysp, I did run into that same problem. Unfortunately there is no way to accurately measure the FPS using just an enterFrame event or a Timer. A good solution is to use flash.utils.getTimer in the same way you’re using the Date object. flash.utils.getTimer lists the number of milliseconds since your swf executed it’s first frame. This is a rather old post, and I have an updated FPS counter at this post:
    Calculating Frames Per Second [Updated Tools/Classes]

    Using flash.utils.getTimer you can specify your animations to move based on time instead of frames, which doesn’t always look the smoothest, but the animation is uniform across platforms and hardware setups.

  11. Using it. Thanks!

  12. I’ve been searching for a good FPS class for an some time now and for some strange reason none of the ones I found was accurate! they actually showed 60fps rather than 10! Luckily then I landed here (: Fantastic job. Thanks for sharing!

  13. Hi, did you have a tutorial about isometric perspective? I’d love your example!

    Regards

  14. [...] 有高手寫了一個FPSBox類別 將FPSBox物件加到場景 就可以觀查fps 下載:Calculating Frames Per Second 說明:AS3 – Calculating true Frames Per Second [...]

  15. Ro – Sorry! I don’t have any tutorials on iso projection. There are plenty out there on the nets though, good luck!

Leave a Reply


Follow me on GitHub
Follow me on Google+
Follow me on Twitter
Thrilling.
EFNX is proudly powered by WordPress
Entries (RSS) and Comments (RSS).