Optimising your code

Optimising your code is about making your script work using fewer resources - this might be less CPU time, less RAM, less hard drive space, or preferably all three at once! When you successfully halve the time it takes to execute a script, you have two options: sit back and enjoy the fruits of your labour, or, perhaps better, make the script do twice as many things .

Either way, it is important to keep in mind that unless your script does some very heavy SQL queries, it is unlikely there will be much noticeable benefit for your end user even after you optimise your script. The reason for this is that most PHP scripts take very little time to execute on today's modern CPUs, which means it is likely each of your scripts take well under a tenth of a second to execute. Compare that to the Internet latency between two hosts, which is between 150-250ms, and the total data transfer time, which is probably about half a second, and you have a total time between request and return of about eight-tenths of a second. If you work hard on your script and increase its speed by a factor of 10, making it execute in just one-hundredth of a second, you will decrease the request-to-return time from eight-tenths of a second to 7.1-tenths of a second. That is, despite your hard work, there is only a 10% noticeable speed difference to the end user, and only then if they have very good time sense.

Is that to say that optimisation is pointless? Not at all - optimisation is always important, as it allows you to lower the resources being used on your server. This in turn allows your server to do more - if your server was running at full capacity, decreasing its load from 100% to 10% is a major achievement, despite the lack of noticeable different from the end user view.

Code optimisation is often a subjective topic. The items listed below are optimisations that I use personally and fully believe they work, however it is possible that you will find other people saying they tried some of these and found them not to work, or, worse, to make their pages run slower. Harrison's postulate covers this situation rather well: "For every action, there is an equal and opposite criticism". So, try these out, see what works for you, and put together your own list of optimisations. Also keep in mind that these optimisations were tested on PHP 5.0.2 - their applicability may be different on other versions.

Finally, a warning: only implement one optimisation at a time. If you try three at once, you may find your code ends up working slower and you will not be able to tell which "optimisation" is hindering your performance. Instead, implement one optimisation, test it out, make sure it is working, then implement another, and another, step-by-step.


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Write your code sensibly >>

Previous chapter: Performance

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.