Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

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]... ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page