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
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.
# For this matter, change:
;include_path = ".:/usr/local/lib/php"
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‘.
- 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.
- 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++
- Tips for faster php-scripts
- Even more faster php scripts
- Extra tips for faster php scripts
- SitePoints blogs on Rasmus Lerdorf at drupalCon Szeged