Premature optimization is the root of all evil
There's loads of ways you can optimize your PHP code, however they are probably NOT the answer to your performance issues.
A wiser man than myself once said:
We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.
This was originally said by Donald Knuth, the father of the analysis of algorithms.
This can also be applied when working with programming languages such as PHP.
Here’s some handy performance tips to point you in the right direction…
Profile your code using a debugger is highly recommended as a way to find those bottlenecks that are slowing it down.
Xdebug is great, it’s open source, highly recommended by PHP experts everywhere, it easily integrates as a PHP extension and with any popular IDE.
Once configured, you can use Xdebug to profile your PHP scripts and determine where the bottlenecks are in your code.
You never know, the issue may not even be down to your code and instead be down to a database or network latency issue.
Upgrade PHP to the latest version. With every release developers have tweaked and optimised the code to improve performance.
So if a particular core PHP function is your bottleneck, perhaps you’re due an upgrade.
Cache everything is the advice given by developers of large websites (such as reddit).
The Memcache module can be used to improve performance by caching database results.
If you use the Smarty template engine, then you can cache pages that way.
Otherwise you may find the Cache_Lite handy.
Ask. Once you’ve profiled your code and discovered your bottleneck the only thing left to do is speak to the community.
The community know best because they’ve already done it.
If you didn’t write it, remember to speak to the author and it’s community first, otherwise try these resources:
Tip: The community is more likely to help you if you lurk a bit first, make some comments and see what it’s all about before start asking questions.
Don’t forget PHP never was and never will be built to be the best.
That’s why Ruby developers make jokes about PHP being better and why facebook developed Hiphop for PHP.
Rasmus Lerdorf (who started PHP) would probably agree that it’s more about getting the job done than “real programming”.
Comments