Performance evaluation in php

This post is inspired by the speech of Rasmus Lerdorf at the drupalcon 2008 in Szeged.  His opinion about php and how it is used was interesting at least.  To give you an example, i quote “If your application is to slow, that is only because your code sucks.”.  I found the slides from this session on the internet as well.bouncy castles

He spoke about perfomance, scalability and security and pointed out that this three things always have to be analyzed together. What is a great performance with the code is not scalable or not secure.  He also compared PHP frameworks on his local machine, showing request transactions per second for each tested framework outputting the most basic “hallo world” page to the browser. The tested frameworks were CakePHP 1.2.0rc2,Symfony 1.1 ,Solar 1.0.0alpha1, Agavi 1.0-beta1,Zend Framework 1.6.0-rc1, CodeIgniter 1.6.3,Prado 3.1.2 and Drupal 6.4 ofcourse. He most liked CodeIgneter because it is the least a framework of them all, leaving you to write more code yourself :) i guess.
Nice to know were his findings on comparing a file with no php tags and a one with Hello world echo to the browser. The static php gave 611.78 trans/sec and the one that needed to interpret php took 606.77 trans/sec. The difference is rather nihil.
All resources to this can be found here.

Some introduction notes

Developers have to be aware that PHP is rarely the bottleneck, because in percentage the parsed server content will only take up 10% from the time the page is requested to the final output in our webbrowser.  A lot of stuff still has to be done by our browsers, such as loading html tags, perform javascript tasks and even adapting the html onload with javascript.  A great tool to overview this matter is YSlow at yahoo or the net tab in the firebug plugin for mozilla firefox.


Relatively fast

This means that simple things like moving a lot of javascript to the footer, will actually speed up the load time dramatically. The browser will simply wait to show html untill all javascript is included. Much can be said about compressors and it is widely known that compressors speed up things.Inflatable Water Games


Fixing the include issue

The include path is the location where php will look for files you ask for with include, include_once, require, require_once and class instantiations with or without __autoload function declared.  Here there are two things you could do to speed things up. Change the include_path (php.ini or dynamically at runtime) and be consistent with the usage of include and include_once.

It is obvious that include_once function will have to do more than the include function, because it has to check if the file parameter has been included yet. So in index files and bootstrap files it will make more sense to use include. And there are other examples.

Secondly you could change the include path to make php look for the files in the logic order to avoid to much checks. Missing an include path is very expensive in PHP.

  1. # For this matter, change: 
  2. ;include_path = ".:/usr/local/lib/php"
  3. # to
  4. include_path = "/usr/local/lib/php:."

So if you include files that are relative, use ./ to include this files, so PHP doesn’t have to look for it first in the php extensions.

Request server time

In stead of always calling a function time(), you could use the $_SERVER['REQUEST_TIME'] in most cases. This is much faster because this is commonly used several times in a bigger project. Time() function is faster than to use date(’U').


Loops are used a lot if you look at a big project. Statistically, a ‘do-while’ is faster than ‘while’ and ‘while’ is faster than ‘for’. Foreach will be the easiest but slowest of them all. For more detailled stats, you can visit ‘Extra tips for faster php scripts‘.

In general
  • To output in PHP, you should use echo instead of print becasuse it is faster. First of all echo is not a function but a statement, and print has a return value and has to be evaluated.  A second tip is to use echo parameters and not as concatenated variables Disney Fairies 5 en 1 Combo.
  1. //This means
  2. echo $var1, ‘ test ‘, $var2;
  3. // is faster than
  4. echo $var1 . ‘ test ‘ . $var2;
  • You will find str_replace faster than eregi_replace. They are both faster than the preg equivalent preg_replace.
  • explode (and implode) functions are faster than to split or preg_split your string data. Ofcourse sometimes you need the regular expression, so you will need split or preg_split
  • ereg is faster than ctype_digit
  • is_numeric is around 15% faster than ctype_digit
  • Define your parameters so that the ones that need most validation and will most-likely be evaluated in your statement to be first. This could feel normal in the sense that we already give less important parameters a default value …
  • Identical comparison statements are much faster than the equation we mostly use. This means === is significantly faster than ==
  • With a 54.45% speed difference, it’s well worth using (int) over intval().
  • Pre-increment is faster than post-increment, as in ++$i will be faster than $i++



faster javascript loops

This entry was posted on Tuesday, September 16th, 2008 at 11:03 am and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

4 Responses to “Performance evaluation in php”

  1. Jack W Says:

    Really interesting

  2. hangman addict Says:

    you should give a look if you like playing hangman… it keeps me busy at work :)

  3. How to Get Six Pack Fast Says:

    The style of writing is very familiar to me. Did you write guest posts for other blogs?

  4. Stalski Says:

    Not really :)