How to speed up PHP

The following methods can help improve scalability with php applications.

1. object code caching

Whenever a request comes in for a php script, it has to go through the compiler and then execute the object code. If this is cached, you skip compiling and go straight to executing, hence a speed increase

There are quite a few object code caching packages, some are free and some are not:

A) Ioncube: http://www.ioncube.com/

B) Zend Encoder: http://www.zend.com/products/zend_safeguard (Not Bad)

C) Turckl MMCache: http://freshmeat.net/projects/turck-mmcache/ (My Fav)

1. Templating systems

Templating systems provide a different type of caching. HTML caching. Templating systems work well when you have static data on one or many of your pages that doesn’t need to be reloaded often. Caching systems also provide a separation of code and html, simplifying the system and making future additions or upgrades easier.

A) Smarty Templates: http://smarty.php.net/ (MY fav)

B) Pear Templates: http://pear.php.net/package/html_template_it/redirected

3. Distributed object caching systems

The most widely used system of this type is memcached (http://www.danga.com/memcached/).

This type of caching makes your database querying faster by caching the majority of your database queries.

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’

Use IP address to access your database. Do this as much as possible as you save the hostname lookup time..

5. Output Compression

Most current browsers now support 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%. This will actually compress your HTML output befor sending to the browser, decreasing your bandwdth usage and speeding up the page loading.

add the following lines to php.ini to enable:

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.

6. Optimize your database and queries.

Query only what you need, avoid SELECTING * and please, please, please index your tables!




191 Responses to “How to speed up PHP”  

  1. No Comments

Leave a Reply