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 engineeering 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.
A failsafe approach to this dilemma is to get into the habit of making good choices about approaching small problems; the benefits will add up in the end. Example 21-1 shows three ways to produce the exact same MD5 hash in PHP 5.1.2.
Hashing three ways
$hashA, $hashB, and $hashC are all 83f0bb25be8de9106700840d66f261cf. However, the third approach is over twice as fast as PHP's basic md5( ) function.
The dark side of optimization with head-to-head tests like these, though, is that you need to figure in how frequently the function is called in your code and how readable and maintainable the alternative is.
For example, in choosing hash functions, if you need your code to run on PHP versions earlier than 5.1.2, you either have to use md5( ) all the time or add a check that, based on PHP's version (and perhaps whether the mhash extension is installed), decides which function to use. The absolute time difference between md5( ) and hash( ) is on the order of a tenth of a millisecond. If you're computing thousands or millions of hashes at a time, it makes sense to insert the extra runtime calculations that choose the fastest functions. But the fraction of a fraction of a breath of time saved in a handful of hash computations isn't worth the extra complexity.
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.
Get started with integrating some easy analysis methods into your development routine. Recipe 21.1 shows you how to time the execution of a function, and Recipe 21.2 expands on that to illustrate timing overall program execution. Learn how to take these simple approaches even farther with Recipe 21.3, which covers the use of debugger extensions for application profiling.
An overview of how to stress test your web site in Recipe 21.4 reminds you that there's more to performance tuning than the code itself'network latency and hardware also play a big role.
One of the most common bottlenecks in many PHP scripts is misuse of regular expressions; Recipe 21.5 explains a few approaches to solving text-matching problems without incurring the overhead of regular expressions.
Recipe 21.6 covers the various PHP accelerators by explaining the common approaches used by the popular open source and commercial accelerators, and how you can expect to benefit from them.