I made a simple haXe profiler

The other day I wanted to see at how many frame-per-second my code was running.

And so I sat down with my keyboard and a glass of water I can only assume was full at the time, and got to work.
Somehow, through a great deal more typing than was strictly necessary, what was to be an FPS display instead became a simple profiler (one that at least also displayed the FPS, I guess).

The thin line near the bottom is a graph of how much time has been spent profiling (red) over the time since the profiler was instantiated (grey). In this case it’s almost entirely grey, which means that the code being profiled isn’t taking up much time.
However, it does assume that the total time spent profiling is the sum of each of the entries. Which, now that I think about it, is pretty dumb. It’s naively assuming that you haven’t nested an entry. I should probably fix that.

But anyway, I use it as follows:

// Instantiate and display it
addChild( new com.gigglingcorpse.utils.Profiler() );
// Profile a code segment
Profiler.start( 'next frame' );
// A code segment
Profiler.stop( 'next frame' );
Profiler.start( 'A bunch of division' );
// A code segment
Profiler.stop( 'A bunch of division' );
Profiler.start( 'A bunch of sqrts' );
// A code segment
Profiler.stop( 'A bunch of sqrts' );

It seemed to work, and everything was cool, and then I went a little crazy with compile conditions because they’re so cool.

Adding and moving profiler start/stop calls between debug and release just seemed like such a pain. A tedious, error-prone pain.
So I changed the code to compile normally if the debug flag was set, and compile as what amounts to an empty class otherwise. It keeps its four public function definitions (new, start, stop, get) but their contents get compile-conditioned out. Also, those functions are inlined when not in debug mode.

My hope is that I can add profiling to a project, and just leave it there with negligible overall effect.

You can find the code at codepaste 2 if it sounds useful to you.

(If it doesn’t..it’ll almost certainly still be there)



  1. Nathan says:

    Nice work. Any idea why it doesn’t compile on the c++ target?