Practical PHP Performance
When it comes to building интернет applications in PHP, performance isn’t typically a major concern. Features, usability and any business concerns are considered a greater priority, as they can be better demonstrated and visualised. Performance graphs don’t make the boss’s day.
So, why should you as a PHP developer, worry about performance? Quite a few reasons, in fact:
1. Efficient code gives you more flexibility with what you do with your application – for example, you can’t exactly throw in a thumbnailing routine if you’ve already maxxed your server.
2. Performance techniques generally line up with best practices, and while best practices are their own justification, they will save you time (and money!) in the long run.
3. On a high-scale application, performance graphs won’t make much of a difference. Still, informing the boss that, thanks to your performance efforts, you can cut server costs significantly, will definitely earn you some credit, and maybe even a raise.
4. Well written (and therefore efficient) are easier to debug.
The potential performance of PHP applications is certainly something to be proud of – at one stage, Digg was handling 200 million page views per month with just three интернет servers and eight database servers. PHP by its nature is one of the fastest интернет scripting languages available, having been more or less written for mod_php, the Apache module used for most PHP installations. Unlike Java, PHP is sufficiently dynamic to run literally as lightweight as you require – for example, you could write a интернет service in just ten lines of code that could easily handle millions of hits a day, as opposed to the overhead of pulling in a significant chunk of enterprise-sized libraries that you probably don’t need.
Performance: is it really what you’re after?
Of course, when any performance considerations come up, it is worth considering if you really want to aggresively manage PHP performance. A common misunderstanding is the difference between performance and scalability. Performance generally refers to the raw efficiency of the application, whereas scalability is the application’s ability to handle greater loads.
As you move into high-traffic интернет applications, especially the large scale installations of big business, it is common to find that performance is not a concern as the business can afford to simply add another server when needed. Still, keeping performance in mind while developing an application can create significant benefits further down the line.
Ten ways to improve your application’s performance
So, without further ado, here are ten ways you can improve the performance of your интернет applications.
You can’t analyze the performance of your entire application yourself. You won’t be watching every single line of code executed. Find software that helps you profile your applications effectively; I find PhpED’s integrated profiler just works, and is great for telling me where script execution is being held up. There are quite a few good tools available, however, especially for non-Windows platforms. A benchmarking tool – such as ab – may also help, as well as a good debugger. A good set of tools will help you identify problems and deal with them for more effective performance optimization.
2. Cache, cache, cache!
For some applications, caching your HTML is the most effective performance optimization. Have you got static content on your site, but find you needlessly regenerate it on every page load? Executing your script could take 5 seconds. Sending over a cache file of the output would take more like 0.005 seconds. Consider taking the entire output of your script, caching it in a simple HTML file and serving it on subsequent requests based on its file modification timestamp. Here’s a simple way to achieve it: