Wednesday, April 12, 2006

10 PHP speed improvement tips for apache

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. Here is an example:
    function &testfunc(&$test_array)
    return $test_array;
    $new_variable =& testfunc($my_testarr);

  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.


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

    By Anonymous Anonymous, at 9:10 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.

    By Anonymous Anonymous, at 9:17 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.

    By Anonymous Anonymous, at 9:37 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

    By Anonymous Anonymous, at 11:28 AM  

  • 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...

    for ($i=0;$i<100000;$i++)

    function test_function_1($input)
    if (count($input)==1) echo 'blah'; // Just to force it to not optimise out the input somehow

    function test_function_2(&$input)
    if (count($input)==1) echo 'blah'; // Just to force it to not optimise out the input somehow

    Zend Studio's profiler reports that test_function_1 took 0.03ms and test_function_2 took 191.97ms. Reversing the order the functions are called had no effect. Pretty astounding really - so don't use references unless there is a reason to.

    This test was performed on PHP5.

    By Blogger Chris Graham, at 4:26 PM  

Post a Comment

<< Home