Jaslabs: High performance Software

High Performance Software

PHP speed hacks

By Justin Silverton

A French Translation can be found here: http://www.vecteur-renaissance.com/vhoen/index.php/2006/04/15/27-10-conseils-pour-accelerer-des-scripts-php

  1. A PHP script will be served at least 5-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts. 
  2. Enable the compression of HTML by putting in your php.ini:output_handler = ob_gzhandler 
  3. Install a PHP caching suite. I have personally used zend (commercial), turck mmcache, and ioncube, and they all work very well. 
  4. Switch from file based sessions to shared memory sessions. Compile PHP with the –with-mm option and set session.save_handler=mm in php.ini. This can drastically improve session management performance.
  5. Another caching technique that can be used when you have pages that don’t change too often is to cache the HTML output of your PHP pages. (a list of template solutions is posted within one of my previous articles).
  6. Use output buffering (See ob_start). This will speed up your PHP code by 5-15% if you frequently print or echo in your code. 
  7. On Windows, FastCGI is the highest performance way of running PHP with Apache. 
  8. In PHP4, objects and arrays should be passed to functions by reference (with &), and everything else by value. In PHP5, objects are already passed by reference.
  9. Don’t use images when text will do. Reduce your image sizes with a software like MacroMedia Fireworks or imagemagick. 
  10. If possible, Run your database server on a different machine. If all static content is served from another server, then you can turn off KeepAlives in httpd.conf to speed up client disconnects. 
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati

8 Comments so far

  1. Anonymous April 17th, 2006 9:10 am

    Why pass the objects and arrays by reference? PHP already does that internally.

  2. AjiNIMC April 17th, 2006 9:17 am

    You can even use the pear cache which allows function caches.

    Compress your output page by removing all unwanted spaces. Optimize your HTML to make it faster.

    Use one css file as once loaded to local machine it can be used. Same applies for JS.

  3. Anonymous April 17th, 2006 9:37 am

    In response to previous comment about PHP already passing arrays and objects by reference.

    This statement is simply incorrect. PHP 4 passes both objects and arrays by value. PHP 5 passes objects by reference but arrays are still passed by value.

  4. Anonymous April 17th, 2006 11:28 am

    (The Original Anonymous Poster)

    PHP4 uses reference counting, so it’ll pass the array and object by “value,” but internally what it does is pass a reference to the variable and increment the reference count.

    If the variable is then changed in the function, PHP will make a copy of it with its own reference count, and decrement the count on the original variable.

    This means that it’s not allocating extra memory for the variables, and you get no performance benefits from passing by reference.

    So if you’re not planning to change the variable, then you have no need to pass by reference. If you’re planning to change the variable, you have to pass by reference anyway.

    (Note: “by reference” and “reference counting” are very different things, and Zend uses the term “aliasing” when talking about the “by reference” type of references.)

    From the horse’s mouth: http://www.zend.com/zend/art/ref-count.php#whatis

  5. Chris Graham May 3rd, 2006 4:26 pm

    I found this blog from Google and as it contadicted with something else I read by a respectable author I had to perform my own test…

    The code…

    $a=array();
    for ($i=0;$i

  6. Jeff Moore July 24th, 2006 11:33 pm

    Don’t pass arrays by reference only for performance reasons. See References in PHP for an explanation of what’s really going on internally.

  7. justin July 25th, 2006 12:14 am

    Here is a direct link to the “references in PHP” article: http://derickrethans.nl/files/phparch-php-variables-article.pdf

Leave a reply