Chapter 22. Performance Tuning


PHP is pretty speedy. Usually, the slow parts of your PHP programs have to do with external resources—waiting for a database query to finish or for the contents of a remote URL to be retrieved. That said, your PHP code itself may not be as efficient as it could be. This chapter is about techniques for finding and fixing performance problems in your code.

There’s plenty of debate in the world of software engineering about the best time in the development process to start optimizing. Optimize too early and you’ll spend too much time nitpicking over details that may not be important in the big picture; optimize too late and you may find that you have to rewrite large chunks of your application.

Optimization doesn’t happen in a vacuum. As you tweak your code, you’re not just adjusting raw execution time—you’re also affecting code size, readability, and maintainability. There are always circumstances that demand screamingly fast execution time. More frequently, however, programmer time or ease of debugging is a more valuable commodity. Try to balance these concerns as you tackle optimization hurdles in your code.

Installing a code accelerator is the best thing you can do to improve performance of PHP. As of PHP 5.5, PHP bundles and builds the Zend OPcache PHP accelerator, but OPcache works for PHP 5.2 and above. This extension is covered in Using an Accelerator.

If your application is still too slow, get started with integrating some easy analysis ...

Get PHP Cookbook, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.