How do NOTICES influence php-scripts performance ?

Flattr this
Tweet this: How do NOTICES influence php-scripts performance ?
Dent this: How do NOTICES influence php-scripts performance ?
Datum: 21.02.2010 23:11:48

Having worked and refactored a of lot ugly code in my life and seen a lot more (often on helping people on php.de) I was wondering how big the impact of NOTICES thrown by php really is.
Since php allows a lot of really bad code and it is easy to turn off showing them (in most cases it is turned off by default and not logged either) I wanted to test some of those things you see almost every day, usage of uninitialized variables and accessing array keys without setting them into quotes.

Since this kind of bad code in most cases are only a small part of the complete applications code, it won't have a huge impact on the total performance of a script, but I wanted to know how much the difference for those code-parts would be.

I made some tests with code which assigns a non-existing variable to another and accesses a non-existing array-key.

I used the php-binary directly (php -f file.php), suppressed the display of the errors by using error_reporting() and init_set with display_errors to false and log_errors to false because displaying and logging all those errors would have messed up the result.

Since only one line is hard to test because execution-time is just too short, I put that stuff into a loop to get some bigger values, 10 Million iterations made a good value for me.


I then ran the code on my notebook (p4 mobile 1.73ghz/2GB RAM debian squeeze php 5.2.12 [32bit OS]) and on my server (i7 920@2.67Ghz/8GB RAM debian lenny php 5.2.12 [64bit OS]).

Code 1: initialized variable vs uninitialized variable

<?php
  error_reporting(E_ALL | E_STRICT);
  ini_set('display_errors', false);
  ini_set('log_errors', false);
  
  $var = 1;
  
  for ($i = 0; $i < 10000000; $i++) {
      $foo = $var;
  }

and

<?php
  error_reporting(E_ALL | E_STRICT);
  ini_set('display_errors', false);
  ini_set('log_errors', false);
  
  for ($i = 0; $i < 10000000; $i++) {
      $foo = $var;
  }
System initialzed variable uninitialized variable  
Notebook 2.36s 13.23s 5.6 times slower
Server 0.91s 4.73s 5.2 times slower

Code 2: array key in quotes vs no quotes

<?php
  error_reporting(E_ALL | E_STRICT);
  ini_set('display_errors', false);
  ini_set('log_errors', false);
  
  $array = array('foo' => 'baa');
  
  for ($i = 0; $i < 10000000; $i++) {
      $foo = $array['foo'];
  }

and

<?php
  error_reporting(E_ALL | E_STRICT);
  ini_set('display_errors', false);
  ini_set('log_errors', false);
  
  $array = array('foo' => 'baa');
  
  for ($i = 0; $i < 10000000; $i++) {
      $foo = $array[foo];
  }
System array key with quotes array key without quotes  
Notebook 3.36s 19.7s 5.8 times slower
Server 1.4s 7.38s 5.2 times slower

It will propably not boost you application to lightspeed, but it can make it a bit faster and will increase your code-quality.

Activating php's error-logging made it ... a lot slower, 25 times slower on my notebook [Slow 120GB 5400RPM 2,5" Notebook-Drive) and about 15 times slower on my server [SATA 750GB RAID-1], but that I only tested with less iterations (10000) because it took so long and logfiles got really big.

So please, use error_reporting(E_ALL | E_STRICT); in your apps and turn display_errors and log_errors on. In production turning off display_errors is okay, but at least keep logging the errors on and check the logs.


Trackbacks (0)

Trackbackurl: http://www.robo47.net/trackback/blogentry/194

Es sind keine Trackbacks vorhanden.


Kommentare (0)

Es sind noch keine Kommentare vorhanden.

Die Kommentare zu diesem Beitrag sind gesperrt.

You liked it ? Link it on your homepage or blog: