Its been quite a long time since PHP has been around and It is still gaining a major share of the web where web application and web site development is concerned. With more and more Developers and Web Masters using PHP as their preferred scripting language, it only makes sense to shed some light, as a trail blazer, to uncorking PHP performance. If you would like your PHP scripts to run at least thirty percent faster, here’s to you.
Most Microsoft and Other Scripting language adopting diehards, have always pointed out the fact that as your scripts keep building up and your web site or web applications keep getting more and more complex day by day, PHP loses ground and starts becoming slower. Now, the flip side of this debate would be; if the above mentioned statement would have been correct, then, why would Yahoo be using PHP, eh?
Yes, Many websites and Biggies, use PHP. Not only because its open source, but also because it is scalable and flexible and performance, today is no more an issue; provided ones know what is required to squeeze every last nana second out of your compile time, fetch time, etc to optimize your scripts and web applications. As compared to its counter parts like the .net Platform, JSP, etc. it only falls cheaper in terms real dollars to adopt PHP; not that we have anything against these languages, but we too like others are searching for an easier, flexible and scalable Web Scripting Mecca.
If done right, complex PHP scripts could be blazing fast. Where most intermidiate PHP based web applications, running into thousands of lines of code, it only goes to show the efficiency of the design and implementation of the Zend Engine that makes PHP tick.
Increasing your PHP Performance, is not making your scripts run faster, but a set off between scalibility and speed. An, unruly, large number of factors influence the performance of a web application or a website. These factors include, in no sequence, database schemas, database performance, server configurations, web application design, and implementation of your web application scripts. We are going to assume that you would have already hammered down the above list, where performance is concerned. So, that leaves the bottlenecks in code and compilation time of your scripts.
The best place to start optimizing for PHP performance would be profiling or commonly known as PHP performance profiling.
Well, according to the Law of Ahmdahl, you cannot put racing tires on a tuk-tuk and expect to burn the road. Direct relation to above law with PHP would only indicate that we need to look at things that are obivious while reviewing your PHP code and Identifying potential bottlenecks. PHP performance profiling, thence, means simply to identify bottlenecks that affect PHP performance and then uncorking these performance bottlenecks.
First step to PHP performance profiling would require you to start looking for such bottlenecks. Which means that you would require information about how your PHP script performs in relation to web browsers and databases. PHP performance profiling, does eventually lead up to running your PHP scripts within a controlled environment, with the environment providing such information to identify bottlenecks affecting PHP performance.
A vast number of tools are available for free, for profiling the performance of your PHP scripts. Some of the most widely used ones include Xdebug, APD, also known as Advanced PHP Debugger, Benchmark, DBG, etc. [List of tools given in resources section below with links]
Selecting a tool from the list, involves asking yourself a question. How serious are you about squeezing every ounce of the cycle out of the code? Depending on the answer, you could either use one single profiler or a multiple of them to detect and compare bottlenecks. All these PHP performance profiling tools, work in different manner and make available different information about your PHP script execution. The most commonly used of the lot being Xdebug and APD.
So, you have identified certain bottlenecks and simplified your code further, where ever required to increase your PHP performance. So whats the next step to PHP performance?
The next step would definitely be optimizing your op code. In normal circumstances your PHP scripts are compiled into Zend Opcodes by the PHP Zend Engine. This Opcode is then excuted, resulting in the formation of an HTML page and this HTML page is what is sent to the browser.
Once the HTML page is sent to the browser, the opcodes are flushed from the memory. That is, the opcodes are cleared from the memory after they have been excuted by the webserver.
Opcodes or Operation codes are binary low level instructions or instruction sets. The PHP opcodes, generated by the Zen Engine can, today, be further optimized using the Zend Optimizer. By just using the Zend optimizer, you can increaser the performance of your PHP scripts, anywhere from, 30-50%. The optimized PHP opcodes, further can be cached. If cached, the PHP opcode after execution is never flushed, thus if the same PHP script/PHP ebpage was to be requested by the browser, the Zend Engine wouldn’t have to compile the script, because the PHP opcode would have been precompiled, optimized and cached! Have a look at the flow chart diagram.
PHP opcode caching can further increase PHP performance form 40-200%.
PHP@ www.php.net
MySQL@ MySQL.com
Zend Engine, Zend Optimizer and Zend Accelerator@ www.zend.com
Benchmark(A PEAR Project)@ http://pear.php.net/benchmark
DBG@ http://dd.cron.ru/dbg
Xdebug@ http://xdebug.derickrethans.nl/
Advanced PHP Debugger@ http://pear.php.net/apd
eAccelerator@ http://eaccelerator.net/HomeUk/
Alternative PHP cache or APC@ http://apc.communityconnect.com/
PHP Accelerator@ http://www.php-accelerator.co.uk/
AfterBurner Cache@ http://www.bwcache.bware.it/
Further reading@ http://phplens.com/lens/php-book/optimizing-debugging-php.php