Chapter 23. Performance
Many people see performance and security as mutually exclusive, particularly in the cost-restricting IT environment in which we currently live. Faced with that choice, it's not surprising that many choose to write very secure—but very slow—PHP scripts. "Fast, cheap, good—pick two" is a compromise we all have to live with to some degree, but you may be surprised to learn that you can speed up your code and add security if you take the time to plan.
Write Your Code Sensibly
"The fastest code is the code that is never executed."
If you are working on something you originally thought would be easy but has ended up being many more lines of code than you originally intended, it is quite possible that your code is getting a little bloated, and more than a little slow. Hoare's law tells us, "Inside every large program is a small program struggling to get out," so you should consider chopping out blocks of code that are outdated, outmoded, replaced, or irrelevant.
In his book The Art of Unix Programming (Addison-Wesley), Eric Raymond says, "The most powerful optimization tool in existence may be the delete key." He also quotes Ken Thompson (one of the most highly respected Unix hackers in the world) as saying, "One of my most productive days was throwing away 1000 lines of code." Very true.
Optimization is down to personal intuition, and is hard to do at first. However, try this out to give you an idea of script performance:
print "Start: ", microtime(true); // ...[snip]... ...