For many of us we have a configuration file for our web applications. In that configuration file there is usually a DEBUG constant that we can turn on to print out helpful debug statements on the screen or to a flat file.

It could be as simple as:


function debug($msg)
{
file_put_contents("debuglog.txt", $msg, FILE_APPEND);
}

debug("starting to load software");


If you do something similar to get data from your applications consider adding a test for the debug value before calling the function directly. If you do you'll get about an 85% speed gain when debug is turned off. Which probably will be 98% of the time your application is running.

I ran some performance tests to back this statement up.

test 1 - using if/else statements around my debug function call. I added 30 of these is/else checks in a script

function debug()
{
file_put_contents("debuglog.txt", $msg, FILE_APPEND);
}

if(defined("debug") && DEBUG==1) {
debug("my message here");
}



test 2 - calling the function directly and testing for debug being on within the function


function debug()
{
if(DEBUG==1) {
file_put_contents("debuglog.txt", $msg, FILE_APPEND);
}
}

debug("my message here");


In my test cases I called the debug function 30 times directly. I ran these tests 1000 times and the averages came to:

Avg for if/else: 0.0002891 seconds total times: 1000
Avg for functions directly: 0.0005356 seconds total times: 1000

calling a function directly was: 85.26% slower on my MAC G4 Powerbook

The same test ran on an 800 Mhz Server (which again I have to support) the test results were 149% improvement in speed using the if() test.

Now if you look at the times .0002891 you might say "wow thanks you dick, I wasted my time reading this to get that measly speed gain???". Well you're right :)
However, I have to make my application run on 266 Mhz machines so eeking out that extra bit really helps speed things up for me. It's not much but hey every little bit count sometimes. If you have a speedy server for maintenance sake it's probably easier to read a bunch of


debug("socket server down!");


than a bunch of:


if(defined("debug") && DEBUG==1) {
debug("my message here");
}


messages.

Ready for More?

Follow Me @jimplush