How To: Make Your PHP Script Run Faster

Justin Silverton on Jaslabs posted an article “how help improve scalability with php applications.” object code caching: Each time a request comes to your server for a php script, it has to go through the compiler and then execute the object code. If this is cached, the 1st step is skipped and you end up with […]

Justin Silverton on Jaslabs posted an article “how help improve scalability with php applications.”

  1. object code caching: Each time a request comes to your server for a php script, it has to go through the compiler and then execute the object code. If this is cached, the 1st step is skipped and you end up with a faster and more responsive script. There are many object code caching packages available on the Internet (some free, some commercial):

  2. Template systems: Template systems provide a different type of caching. Content caching. Template systems work well in a situation where there is static data on one or many of your pages that doesn’t have to be reloaded. Caching systems also provide a separation of code and html, which will not only improve completion time of the overall project, but make it easier for future improvments. Most template systems for php are available for free:

  3. Distributed object caching systems: The most widely used system of this type is memcached (http://www.danga.com/memcached/). This type of system makes your overall site faster by caching the majority of your database data into a large memory pool.

    an interesting excerpt from their site:

    Danga Interactive developed memcached to enhance the speed of LiveJournal.com, a site which was already doing 20 million+ dynamic page views per day for 1 million users with a bunch of webservers and a bunch of database servers. memcached dropped the database load to almost nothing, yielding faster page load times for users, better resource utilization, and faster access to the databases on a memcache miss.”

  4. PHP variables that can be set:

    variables_order = ‘GPC’
    register_argc_argv = ‘Off’
    register_globals = ‘Off’ (this is a good idea to keep off for security purposes as well)
    always_populate_raw_post_data = ‘Off’
    magic_quotes_gpc = ‘Off’

  5. Disable Error Logging. This is a good idea to keep on when you are developing your scripts, but it has been known to decrease overall performance.

    Use IP address to access your database. Although this is sometimes not possible, you will get a slight boost in lookup speed if the IP address is used to access your database rather than its hostname.

  6. Output Compression: Almost all browsers these days support something called gzip compression. Gzip compression can decrease the overall size of your output by up to 80%, but with a tradeoff: cpu usage will go up by around 10%. The benefit of using this compression type is the fact that not only will your bandwidth be decreased, but your pages will load faster.

    enabling it in php (add the following lines to php.ini):

    zlib.output_compression = On
    zlib.output_compression_level = (level) (where level is 1-9. Youy may want to try different values to see what is best for your system).

    if you are using apache, you can also enable the mod_gzip module. It is highly configurable, with the ability to modify output based on MIME types, files, or browser settings.

  7. Other things that may help: when using a database, only retrieve the data that you are actually going to use. This may sound like a no-brainer, but I have often times worked on projects where the original programmer used (select * from mytable) when they could have used (select fieldIneed from mytable).

    index database tables whenever possible

Learn more about this Here

specific language tricks

An interesting blog article I found mentions many interesting tricks that can be used: http://ilia.ws/archives/12-PHP-Optimization-Tricks.html

an article on zend.com about measuring performance: http://www.zend.com/zend/trick/trick-optimizing-php.php

Jaslabs

How To, Make Your PHP Script Run Faster