Domain51_MicroBench Released

Happy New Year to everyone. Hope the holidays were good to everyone. Mine were good; plagued by some sick time, but good nonetheless. Now on to what this post is about…

Ever do the same bit of code over and over again and every time you do it think you should write something to simplify it? I keep doing that with micro-benchmarks. For those of you who aren’t familiar with the concept, you take a snippet of code and iterate over it N times, the change it to use another algorithm, run it again, and compare the results. For a formal benchmarking technique it’s basically useless, but to just get a quick insight as to which of two snippets are faster it’s pretty useful.

The code to track the time taken looks the same on every test. Do any setup, start the timer, iterate over the code, stop the timer, and calculate and display the results. Today when I sat down to write a few tests out to compare include_path usage in a couple of different scenarios to see what the raw performance was like I decided to whip up a quick package for creating those for me, and Domain51_MicroBench was born.

It can be installed the normal ways. Do a channel-discover on pear.domain51.com, then install d51/Domain51_MicroBench-devel. If that sentence doesn’t make sense to you, this package is probably too rough around the edges for you still. Wait for an alpha release.

There’s no documentation or examples other than what I’m about to write, so pay close attention… :-) You write your normal PHP file with two marker comments in it:

<?php
// setup
$path = dirname(__FILE__);
$paths = array();

// test
$paths[] = $path;

That’s it. All of the setup code goes after “// setup”, all of the test code goes after “// test”. With that file handy, run d51_microbench /path/to/file.php from the command line and you’ll a ”file-bench.php” file generated for you with all of the repetitive parts added for you.

As a word of caution, this doesn’t replace a profiler and is best used with one. Get your profiler out, start checking your script to figure out what’s slow. Once you’ve isolated the slow code, then, and only then, start doing some checks of various algorithms with something like Domain51_MicroBench to figure out how to make it faster. A 75% reduction in speed on a part of your code that takes 0.05% of the time translates into saving for over 1/100th of one percent of an execution cycle. Kind of a waste unless that’s the last spot to speed your code up at.