Performance
Performance, particularly on busy sites, can be critical - after all, if you can speed up your code by 10%, that decreases your hardware load by 10%, saving you the need to upgrade. There are a number of ways you can improve the performance of your scripts, and we will be covering as many as have space for. We will also be dispelling various myths about optimisation, and hopefully by the end of this chapter you will be confidently about to re-write fundamentally flawed algorithms, tune implementations of good algorithms, make your MySQL queries fly, and more.
Before we begin, I would like to make it quite clear that optimisation is the process of improving performance of your code, whether to use less space or run faster - it is usually a trade-off. Optimised code is not necessarily "perfect code", it is just better than unoptimised code.
Furthermore, there is rarely if ever such a thing as "too fast". In my spare time, I have been working on my own pet project: a gigantic online PHP strategy game. Actions occur at round end, which is triggered by a cron job every ten minutes. With a thousand dummy users in the system, round end takes over seven seconds, during which database writes are locked so that people cannot make changes. In the past I have spent hours and hours just to cut one part of that round end from 0.002 seconds to 0.001 seconds per player - it might not sound like a lot, but as far as I am concerned every single second counts. If you have tried out half of the recommendations here and find you have reduced the run-time for a script from four seconds down to one second, don't stop there - go for the fastest code you can get.
Topics covered in this chapter are:
-
Increasing performance by optimising your scripts
-
Increasing performance by optimising your SQL
-
Increasing performance by optimising your server
-
Caching PHP scripts
-
PHP the CGI vs. PHP the Apache module
Chapter contents
- 18.1. Optimising your code
- 18.1.1. Write your code sensibly
- 18.1.2. Use your tools wisely
- 18.1.3. Avoid functions for maximum performance
- 18.1.4. Use the Zend Optimizer
- 18.1.5. Use a PHP code cache
- 18.1.6. Read the manual carefully
- 18.1.7. Get your loops right first
- 18.1.8. Pre-increment where possible
- 18.1.9. Don't think that using references will lower your RAM usage
- 18.1.10. Be wary of garbage collection, part 1
- 18.1.11. Be wary of garbage collection, part 2
- 18.1.12. Listen to all errors, big and small
- 18.1.13. Keep up to date
- 18.1.14. Cache array data
- 18.1.15. Compress your output
- 18.1.16. Don't use CGI
- 18.1.17. Don't use dl()
- 18.1.18. Debug your code
- 18.1.19. Cache your pages
- 18.1.20. Use persistent connections
- 18.1.21. Take advantage of new features
- 18.1.22. Compile right
- 18.1.23. Ignore the myths
- 18.1.24. Avoid mod_access if you can
- 18.1.25. Don't fret about Apache 2.0
- 18.2. Optimising your SQL
- 18.2.1. Prioritise your data
- 18.2.2. Optimise your tables
- 18.2.3. Select as little data as possible
- 18.2.4. Use shorter queries where possible
- 18.2.5. Use the EXPLAIN statement
- 18.2.6. Change your hardware
- 18.2.7. Choose your data types carefully
- 18.2.8. Size vs. Speed
- 18.2.9. Declare fields NOT NULL
- 18.2.10. Load data intelligently
- 18.2.11. Consider splitting off variable-length fields
- 18.2.12. Be wary of locks
- 18.2.13. Consider switching OS
- 18.2.14. Spot slow queries
- 18.2.15. Perform joins carefully
- 18.2.16. Index your data
- 18.2.17. Make sure your indexes are being used
- 18.2.18. Normalisation
- 18.2.19. Upgrade MySQL
- 18.2.20. Increase your buffers
- 18.2.21. Keep tabs on your server
- 18.2.22. Lock your tables when appropriate
- 18.2.23. Don't rely on automatic type conversion
- 18.2.24. Benchmark, benchmark, benchmark!
- 18.2.25. Know MySQL's strengths
- 18.3. Server
- 18.4. PHP Accelerators
- 18.5. Caching PHP
- 18.6. PHP as a CGI or a module?
- 18.7. Size of scripts
- 18.8. Summary
- 18.9. Exercises
- 18.10. Further reading
- 18.11. Next chapter



Copyright 2010 Future Publishing Limited (company
registered number 2008885), a company registered
in England and Wales whose registered office is at
Beauford Court, 30 Monmouth Street, Bath, BA1 2BW, UK