Hudzilla.org - the homepage of Paul Hudson
Contents > Performance > Optimising your code Wish List | Report Bug | About Me ]

18.1.1     Write your code sensibly

This is NOT the latest copy of this book; click here for the latest version.

"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.

I've quoted from it already, but Eric Raymond's "The Art of Unix Programming" is just so damn right about this sort of thing that I can't help but quote from it again. Raymond says that, "the most powerful optimisation tool in existence may be the delete key", and even 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."

This is very much down to personal intuition, and is often very hard to do at first. However, try this kind of thing out to give you an idea of script performance:

<?php
    
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.

When dealing with performance benchmarking, however, you need to be wary - it has been said that "premature optimisation is the root of all evil". That is, there are many optimisations 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.

Find a happy medium - test your scripts now and again, and note where things are running poorly. However, hold your optimisation off as long as possible.





<< 18.1 Optimising your code   18.1.2 Use your tools wisely >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 06 Sep 2008

print "Start: ", microtime(true);

the comma needs to be a period.

A PHP User - 06 Sep 2008

Very useful, thank you!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is six plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow