Jeff Atwood (@codinghorror) recently twittered an url to a blogpost from the guys at OpenX.org about their optimization tactics used for their OpenAds framework. All I can say is that the state of PHP at this moment is abysmal. I’ll explain why.
The writer speaks about three rules they applied when optimizing their code, being:
- Forget about so-called “common sense” and trust only a profiler;
- Use the minimalist approach;
- Optimize for real life scenarios.
I can only agree with these rules, as they should count for all programming languages. I’m a strong follower of the YAGNI approach. Don’t build things you don’t need now or in the very near future. Also, when optimizing (which should not be done in advance, of course), you have to measure to find your bottlenecks. The third rule makes sense as well. However! It’s not the developers I’m criticizing here. It’s the language.
The statistics were very interesting and we learned some things about PHP from them. It seems that when we started more than 80% of the delivery time was wasted just on including files. In other words, PHP didn’t do anything useful in that time. Reducing the number of included files gave us such a good performance boost that we even decided to write a delivery merger tool to put all the code from included files together into a single file.
Wait, what? Basically what they’re saying is that dividing your code into separate files (with, hopefully, descriptive names) is actually a big performance hit in PHP. If you want to go for performance, don’t bother dividing your classes over different files. Who would ever do that? I’d have to switch between different files in my editor when developing! (/sarcasm)
And, the other gem:
Another good example of a PHP “quirk” is the way PHP handles constants. It was one of the major factors affecting performance. Just removing all the constants allowed us to improve the performance by almost 2x.
So, by throwing out everything we’ve learned (don’t use magic numbers, extract your strings to constants if possible), we improve the performance twofold. That’s 100% faster! Now, I know PHP has a few annoyances (like array functions which take the needle first in one function, but last in another function), but this is downright appalling. How can we develop proper applications when a language is so very broken?