NOTE
This post has been previously published on 2007/Oct/17: due to my move to another server i’m now in the process to manually recover the comments for this article.
Fixed!

I’m currently working on an early-stage, more of a proof-of-concept thing, but i needed to know more about the code performances: googling for an ActionScript profiler just showed up ASProf, an AS2 profiler. So i decided to port to AS3 the C++ profiler i wrote for Aqua, the cross-platform framework i was working on until April: the original C++ implementation has been developed after being inspired by an article of Steve Rabin in the Game Programming Gems 1 book, that’s a nice one to have on the shelf together with some other great books of which i could post something about them later.
It is always wise to remember that if you are serious about code profiling, you’ll be better to search for some professional tools: in the C++ arena my choice would be this one, but if you plan to have lunch and dinner for the next couple of months give Eletric’s one a try, it really deserves a look.
Unfortunately for the AS3 world, i just haven’t found anything really usable for it up to now, so i just thought to release it under a zlib/png license and some hints on how it works here.


ProfilerConfig.Width = stage.stageWidth;
ProfilerConfig.ShowMinMax = true;
prof = new Profiler( 32 );
addChild( prof );

Setup some custom configuration parameters and then create a Profiler and don’t forget to add it to your local render queue: the Profiler its a Sprite and it will render the results right there, all you have to do now is to profile some code:


var i: int = 0;
var it: int = 1000000;

prof.beginProfiling();

i = it;
prof.begin( "fadd" );
while( i-- ) { fRes += 1.; }
prof.end( "fadd" );

i = it;
prof.begin( "iadd" );
while( i-- ) { iRes += 1; }
prof.end( "iadd" );

i = it;
prof.begin( "fsub" );
while( i-- ) { fRes -= 1.; }
prof.end( "fsub" );

i = it;
prof.begin( "isub" );
while( i-- ) { iRes -= 1; }
prof.end( "isub" );

prof.endProfiling();

With another couple of lines of code you’ll end up with something like this:

Cool graphing for your function-timing needs ;)
And this is it!
A thing i may point out is that you can always group code fragments belonging to your most logical schema by wrapping the same ProfileNode around the different fragments; here i’m grouping math operations by their type, just to keep the call-graph a little bit organized:


var i: int = 0;
var it: int = 1000000;

prof.beginProfiling();

i = it;
prof.begin( "float", true );
prof.begin( "fadd" );
while( i-- ) { fRes += 1.; }
prof.end( "fadd" );
prof.end( "float" );

i = it;
prof.begin( "int", true );
prof.begin( "iadd" );
while( i-- ) { iRes += 1; }
prof.end( "iadd" );
prof.end( "int" );

i = it;
prof.begin( "float", true );
prof.begin( "fsub" );
while( i-- ) { fRes -= 1.; }
prof.end( "fsub" );
prof.end( "float" );

i = it;
prof.begin( "int", true );
prof.begin( "isub" );
while( i-- ) { iRes -= 1; }
prof.end( "isub" );
prof.end( "int" );

prof.endProfiling();

That’s all there is to it, hope it will be useful!
Grab the Profiler here and take a look at the demo code used for this post.