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.
4 Responses
Mister Troll
December 12th, 2007 at 12:15 1Hi,
I’ve tried your profiler, found bug but it is very usefull. Thanks.
There is a problem with levels and parentNodes when recursion occurs.
Thanks again, it’s a good job.
Mister Troll.
Manuel
December 13th, 2007 at 9:05 2Ehi thank you for reporting that: i’ll give it a look at it but if you modified to work with recursion too i’ll be glad to publish your version since i’m quite busy; it should detect a recursion by looking up the node’s name and forcing “end”-”begin” calls but what do you expect to be represented graphically?
Hunter Loftis
October 25th, 2008 at 17:16 3Damn useful. I made some changes here:
Public Class ProfilerConfig { …
public static var GlobalProfiler:Profiler = null;
}
so I can use this:
ProfilerConfig.GlobalProfiler = new Profiler(32);
addChild(ProfilerConfig.GlobalProfiler);
So now ProfilerConfig.GlobalProfiler will be the same across all the classes in the project.
Thanks for building this great system! I wanted to diagnose performance bottlenecks and planned on writing something similar. Finding yours saved me tons of time! Well done. Also, nice interface - better than I would have done.
Senne
December 8th, 2008 at 18:55 4Thank you for a wonderful tool!
Leave a reply