Do you use debug statements in PHP? Speed them up by 85%
August 17, 2006
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:
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
test 2 - calling the function directly and testing for debug being on within the function
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
than a bunch of:
messages.
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); } 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:
debug("my message here"); }
messages.
random says:
August 17, 2006 @ 21:30 — Reply
defined()seemed pretty slow when I tested it, so I just assume it's always defined. Might also be neater to use a short notation:do_something(); DEBUG==1 and debug('something'); do_something_else();Tony says:
August 17, 2006 @ 23:01 — Reply
assuming that the constant is always defined, wouldn't [code]if( DEBUG==1 && defined("debug") )[/code] be slightly faster? When DEBUG is turned off (98% of the time?), the conditional will short-circuit and not bother with evaluating if 'debug' is defined or not.
Markus Tacker says:
August 18, 2006 @ 01:49 — Reply
Just drop the check if DEBUG is defined altogether. It's useless. Just define DEBUG with true or false and check if(DEBUG) { }
Dennis says:
August 18, 2006 @ 03:41 — Reply
What about conditionally defining the function? E.g.: if(DEBUG==1){ function debug($msg){ ... } } else { function debug($msg){ } }
Stefan Scholl says:
August 18, 2006 @ 06:17 — Reply
But why use PHP at all if the program forces you to fight for a few milliseconds?
John Herren says:
August 18, 2006 @ 06:36 — Reply
Just a reminder, it's not a good idea to simply check a constant with if(DEBUG). If you accidentally forget to define the constant, that code will give you an undesired result. Remember that undefined constants get evaluated as the string of the name of the constant. For example, run this code without defining the constant first: if(DEBUG){echo DEBUG;}
Jim Plush says:
August 18, 2006 @ 08:18 — Reply
John is correct, it's just a safety check to do is_defined. Otherwise DEBUG is the string "DEBUG" which would evaluate to true and cause some unwanted behavior. As far as fighting for a few milliseconds.... PHP is robust enough to allow us to do many things much more rapidly than a compiled langauge for this particular project. All the heavy processing is done in a c++ based "server" component. PHP is a thin wrapper to route traffic in our system via TCP socket servers as well as provide a nice front end for a user interface.
Marc Gear says:
August 24, 2006 @ 04:34 — Reply
i do something similar except I like to see where the debug came from sometimes: debug('Some debug message at line '.__LINE__.' in '.__FILE__);
n0n@m3 says:
August 28, 2006 @ 02:07 — Reply
In my opinion, the best way is : if(is_defined(DEBUG) and DEBUG == 1) function Debug($MSSG) { // DO SOMETHING WITH MSSG } else function Debug ($MSSG) {// Do nothing with the mssg}
blu ray ripper says:
April 18, 2010 @ 04:26 — Reply
Comment pending moderation
convert hd video says:
May 6, 2010 @ 02:03 — Reply
Comment pending moderation
642-067 says:
May 19, 2010 @ 00:54 — Reply
Comment pending moderation
virbram five fingers says:
June 4, 2010 @ 20:35 — Reply
Comment pending moderation
christian louboutin says:
June 5, 2010 @ 03:01 — Reply
Comment pending moderation
LOUIS VUIttON STOre says:
June 9, 2010 @ 23:16 — Reply
Comment pending moderation
air max says:
June 24, 2010 @ 02:29 — Reply
Comment pending moderation
rolex watches says:
June 24, 2010 @ 03:03 — Reply
Comment pending moderation
louis vuitton handbags says:
June 25, 2010 @ 03:09 — Reply
Comment pending moderation
louis vuitton handbags says:
June 26, 2010 @ 21:20 — Reply
Comment pending moderation