PHP Optimization

August 10th, 2014 Leave a comment Go to comments

Remember, some of these are negligible, and can change at any time. The PHP engine may optimize things differently as new versions come out. A lot of these could probably just be considered "micro-optimizations" and usually are not worth substantial work to implement.

Part of coding is maintenance of the code. Making things overly complex to cut down on speed can sometimes be more of a headache than it is worth!

Optimization priorities/levels are *low* *medium* *high* below, with references. I hope to continue to build this case with examples and any changes/additions.

This page really needs to be cleaned up, I know 🙂

  • ' is faster than " with strings, because it does not process $variables [1] *low*

  • echo is faster than print [1] *low*

  • BAD:

    for($i=0;$i< sizeof($array); $i++)

    Why? It will call sizeof() for each iteration!

    BETTER: [1, 6] *medium*

    for($i=0, $k=count($j); $i< $k; $i=$i+1)

    EVEN BETTER (when needing string concatenation) [3] *medium*

    for ($i=0, $k=count($j); $s=''; $i<$k; $i=$i+1)

    EVEN BETTER (looping through an array) - needs no additional temporary or incremented variables *high*

    foreach($array as $value)

    (if you only need values)

    foreach($array as $key => $value)

    (if you need the keys as well)

  • for($i=0; $i++< $count;) might be the fastest for loop (not tested) [4]

  • strcmp (and related) > substr > pcre > ereg *high* - ereg is being deprecated anyway!

  • strpos > preg_match > ereg [3] *high* - ereg is being deprecated anyway!

  • str_replace > preg_replace > ereg_replace typically [3] - ereg is being deprecated anyway! *high*

  • cache- and structure-wise, arrays > apc > files on local filesystem > memcached [2] *high*

  • ++$i is faster than $i++ - 3 opcodes instead of 4 [1] *medium*

  • if(!isset($foo{5})) is faster than if(strlen($foo) < 5) [1]

  • to search an array $value = isset($foo[$bar])) ? $foo[$bar] : NULL; is faster than in_array() [1]

  • if (42 == $foo) is better (maybe faster) than if ($foo == 42) [1]

  • true is faster than TRUE [1]

  • concatenating strings with single quotes ('foo '.$string) is faster than "foo $string" [5]

  • Switch is better than long lists of if/else statements [6]

  • if(empty($var)) is much faster than if($var == '') (reported to be 20% faster)

  • MySQL: Selecting the primary key and limiting to 1 result reported to be 10x faster than using COUNT(*) [7]

  • if(isset($myArray[$key])) > if(array_key_exists($key, $myArray)) > if( $myArray[$key]) - the last one is not really legitimate anyway [8]

  • mysqli_stmt > mysql_query > mysqli_query > mysqli_multi_query [9] - NOTE: is this still true for mysqlnd? What about PDO then too?

  • TODO: branch prediction - is it better to go TRUE or FALSE first?

References

  1. http://ilia.ws/archives/12-PHP-Optimization-Tricks.html
  2. http://www.mysqlperformanceblog.com/2006/08/09/cache-performance-comparison/
  3. http://phplens.com/lens/php-book/optimizing-debugging-php.php
  4. http://cosminb.blogspot.com/2004/09/performance-tweaking-for-vs-while-vs.html
  5. http://blog.libssh2.org/index.php?/archives/28-How-long-is-a-piece-of-string.html
  6. http://www.php.lt/benchmark/phpbench.php
  7. http://mysqldba.blogspot.com/2007/06/to-count-or-not-to-count.html
  8. http://news.php.net/php.general/264211
  9. http://www.johnjawed.com/benchmarks/