PHP Optimization Tips – The Balkan way!

Author: seven March 31, 2007

Lately I became obsessed with a speed and optimization of PHP web applications. Namely, we are currently working heavily on a new release of voodoo [vudu] (a content engine we built using our in-house developed framework which utilizes few common PHP design patterns). I guess that ActionScript optimization got under my skin and now it’s kicking in into PHP front. :) We are reinventing the wheel, but hey – what else would we do? Play Quake all day long? :)

Soooo, I did some benchmarking, I did some reading, and this is what I’ve came up so far:

  1. If a method can be static, declare it static. Speed improvement is by a factor of 4
  2. Avoid magic like __get, __set, __autoload
  3. require_once() is expensive
  4. Use full paths in includes and requires, less time spent on resolving the OS paths
  5. See if you can use strncasecmp, strpbrk and stripos instead of regex
  6. preg_replace is faster than str_replace, but strtr is faster than preg_replace by a factor of 4
  7. Error suppression with @ is very slow
  8. $row[’id’] is 7 times faster than $row[id]
  9. Single quotes are faster than double quotes
  10. (42 == $foo) is faster then if ($foo == 42)
  11. ++$i is faster than $i++
  12. true is faster than TRUE, And 1 is even faster than true
  13. Calling isset() happens to be faster then strlen()
  14. While is faster than do while:
    while (--$i) { //do nothing }

    as opposed to

    do { //do nothing } while (--$i);

    And this performs even better:
    for ($i=0; $i++<$count; ) { ... }

  15. Using persistant over non-persistant DB connections in mysql can increase number of requests per second by 41%
  16. You can save yet another 40% of time spent for parsing and compiling by using an opcode cache like APC
  17. References do not provide any performance benefits for strings, integers and other basic data types. In contrast, functions that accept array and object parameters have a performance advantage when references are used. This is because arrays and objects do not use reference counting, so multiple copies of an array or object are created if "pass by value" is used. So the following code:
    function ObjRef(&$o) { $a =$o->name; }
    is faster than:
    function ObjRef($o) { $a = $o->name; }
  18. In PHP 5, all objects are passed by reference automatically, without the need of an explicit & in the parameter list. PHP 5 object performance should be significantly faster.
  19. Fastest PHP Input filtering comes as a filter extension built in PHP 5.2 and is available for 5.1 as a separate download. There is a good tutorial on zend devzone
  20. The ctype extension offers a series of function wrappers around C's is*() function that check whether a particular character is within a certain range. Unlike the C function that can only work a character at a time, PHP function can operate on entire strings and are far faster then equivalent regular expressions.
    preg_match("![0-9]+!", $foo);
    vs
    ctype_digit($foo);
  21. Internally arrays are stored inside hash tables when they array element (key) is the key of the hashtables used to find the data and result is the value associated with that key. Since hashtable lookups are quite fast, you can simplify array searching by making the data you intend to search through the key of the array, then searching for the data is as simple as $value = isset($foo[$bar])) ? $foo[$bar] : NULL;. This searching mechanism is way faster then manual array iteration, even though having string keys maybe more memory intensive then using simple numeric keys. Second example is is roughly 3 times faster.

    $keys = array("apples", "oranges", "mangoes", "tomatoes", "pickles");
    if (in_array('mangoes', $keys)) { ... }

    vs

    $keys = array("apples" => 1, "oranges" => 1, "mangoes" => 1, "tomatoes" => 1, "pickles" => 1);
    if (isset($keys['mangoes'])) { ... }

Some tips may be completely out dated or completely wrong due to different hardware/software configurations on systems used for benchmarking.

And for over-obsessed characters - I've found interesting articles about how stuff is handled differently in php5.1 and 5.2 opcode - compiled variables and strings.

Sources: moskalyuk.com, whenpenguinattacks,
iBlog, phpLens, wikimedia, phpbench, php.net, php.net - migration,

Author
seven
CEO/CTO at Nivas®
Neven Jacmenović has been passionately involved with computers since late 80s, the age of Atari and Commodore Amiga. As one of internet industry pioneers in Croatia, since 90s, he has been involved in making of many award winning, innovative and successful online projects. He is an experienced full stack web developer, analyst and system engineer. In his spare time, Neven is transforming retro-futuristic passion into various golang, Adobe Flash and JavaScript/WebGL projects.

    Leave a Reply

    Your email address will not be published. Required fields are marked *