Section 23.1. Write Your Code Sensibly


23.1. 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]...     print "End: ", microtime(true); 

This allows you to time the execution of your script, or at least certain parts of your script. If you see something running particularly slowly, it may be because your implementation is bad, or because your actual algorithm is faulty.

There are many optimizations that can be implemented to make your code run faster/smoother; however, most of them also make the code harder to read and/or edit.



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net