PHP, Zend Framework and Other Crazy Stuff
Archive for March 10, 2009
Can’t see the forest for the trees? Quit micro-optimising and try again.
Mar 10th
There’s a humourous story about a naive developer who spent months trying to speed up a single PHP application. The punchline is that it was me
. I was young and foolish, and spent all that time working with legacy PHP3 source code seeking optimisations. My inexperience wasted months (it was an open source app – not on the job thank you!).
Alex Netkachov’s PHP micro-optimization tips post to his blog creates a list of “micro optimizations” which include variations showing the speed of operations between loosely related similar alternatives. For example, a function is faster than a class static method which is in turn faster than an object method call. This is all well and true, but such optimisations (unless you are fantastically popular on the order of Yahoo or Google) rarely provide a measureable benefit.
When I was writing Performance Optimisation For Zend Framework Applications for “Zend Framework: Surviving The Deep End”, I highlighted this by reference to the practice of only optimising when you are certain to achieve a measurable improvement (easily assessed using xdebug). Micro optimisations rarely make a dent in a real application since there are countless other avenues of optimisation far more worth the effort. Don’t be like me in the 90s – don’t lose sight of the real optimisations by getting lost in applying micro optimisations.
This is not to blame Alex Netkachov whose list is actually dead on target! But to emphasise his own statement that application optimisation can offer a far greater benefit. I actually agree with his sentiment quite a bit, and I think it’s fair to say that his micro-optimisations, to an extent (e.g. static methods are a curse when testing which offsets any speed benefit by far), are the kind of stuff experienced developers apply without thinking – an automated part of their programming style balanced against the needs of object oriented applications.
Is this a contradictory stance? I don’t think so. I started using echo with comma delimited strings because someone pointed out it was faster than printing a concatenated string! The most sensible micro-optimisations are ingrained habits – not something you need to apply after the fact when it is way too late, and honestly, offer little added benefit for the extra cost of deliberately adding them.
