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

  1. 18.1. Optimising your code
    1. 18.1.1. Write your code sensibly
    2. 18.1.2. Use your tools wisely
    3. 18.1.3. Use some functions carefully for maximum performance
    4. 18.1.4. Use the Zend Optimizer for older PHP versions
    5. 18.1.5. Use a PHP code cache for newer PHP versions
    6. 18.1.6. Read the manual carefully
    7. 18.1.7. Get your loops right first
    8. 18.1.8. Pre-increment and post-increment aren't the same
    9. 18.1.9. Don't think that using references will lower your RAM usage
    10. 18.1.10. Be wary of garbage collection, part 1
    11. 18.1.11. Be wary of garbage collection, part 2
    12. 18.1.12. Listen to all errors, big and small
    13. 18.1.13. Keep up to date
    14. 18.1.14. Cache array data
    15. 18.1.15. Compress your output
    16. 18.1.16. Don't use CGI
    17. 18.1.17. Don't use dl()
    18. 18.1.18. Debug your code
    19. 18.1.19. Cache your data with memcache
    20. 18.1.20. Use persistent connections
    21. 18.1.21. Take advantage of new features
    22. 18.1.22. Compile right
    23. 18.1.23. Ignore the myths
    24. 18.1.24. Avoid mod_access if you can
    25. 18.1.25. Make sure you optimize Apache
  2. 18.2. Optimising your SQL
    1. 18.2.1. Prioritise your data
    2. 18.2.2. Optimise your tables
    3. 18.2.3. Select as little data as possible
    4. 18.2.4. Use shorter queries where possible
    5. 18.2.5. Use the EXPLAIN statement
    6. 18.2.6. Change your hardware
    7. 18.2.7. Choose your data types carefully
    8. 18.2.8. Size vs. Speed
    9. 18.2.9. Declare fields NOT NULL
    10. 18.2.10. Load data intelligently
    11. 18.2.11. Consider splitting off variable-length fields
    12. 18.2.12. Be wary of locks
    13. 18.2.13. Consider switching OS
    14. 18.2.14. Spot slow queries
    15. 18.2.15. Perform joins carefully
    16. 18.2.16. Index your data
    17. 18.2.17. Make sure your indexes are being used
    18. 18.2.18. Normalisation
    19. 18.2.19. Upgrade MySQL
    20. 18.2.20. Increase your buffers
    21. 18.2.21. Keep tabs on your server
    22. 18.2.22. Lock your tables when appropriate
    23. 18.2.23. Don't rely on automatic type conversion
    24. 18.2.24. Benchmark, benchmark, benchmark!
    25. 18.2.25. Know MySQL's strengths
  3. 18.3. Server
  4. 18.4. PHP Accelerators
  5. 18.5. Caching PHP
  6. 18.6. PHP as a CGI or a module?
  7. 18.7. Size of scripts
  8. 18.8. Summary
  9. 18.9. Exercises
  10. 18.10. Further reading
  11. 18.11. Next chapter

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: Optimising your code >>

Previous chapter: Next chapter

Jump to:

 

Home: Table of Contents

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