There is a lot to be said, when it comes to optimizing PHP scripts for speed and personally, I don't even know the half of it. However, one pet peeve of mine is useless speed optimizations. Every now and then I happen upon an article that discusses speed optimizations when comparing something like ++$i; versus $i++;. These are both so blazingly fast operations, that it honestly doesn't make a big difference. When you have an operation that that takes maybe a microsecond to execute, it honestly doesn't make a big difference if you take 20% off that operation. Even when you multiply that by thousands and thousands of visitors, you've still all the other code that's much bigger worry.
These are something I would like to call useless optimizations. My biggest problem with them, is that often people present these optimizations as facts that should be followed regardless of everything else. There are, however, so many other things to consider when writing PHP code, such as code maintainability and purpose of different functions and structures.
One example of common pointless speed optimization is the comparison of different loop types, such as while, for, do ... while and foreach. Not only is the speed differences mostly negligible, but each loop type serves very different purpose. Of course it's obvious that while is faster than for and foreach is the slowest. But what's the point of writing writing following code, when you could just use simple for instead?$i = 0;
while ($i < $count)
{
$i++;
}
The for loop would be much better for one particular reason: It was meant for this. When people read the code, it's much easier to understand what it's supposed to do, when you are using the correct code structures (in addition, it's easier for you to understand in after few years). Good coding practices are worth a lot more than pointless little optimizations.
Most importantly, comparing speed of different functions often misses the general point of writing efficient PHP code. It's not about choosing the faster function in one particular case, it's about understanding why some functions are better in some cases than others. I'm not saying all speed optimizations are useless. Quite often, best approach is the fastest approach, but it's not something you should sacrifice well structured code for.
On top of this all, the simple fact is that load is rarely balanced evenly among scripts. There's usually some spesific bottleneck in code, that's taking most of the resources in script execution. Optimizations are usually waste of time, if you are optimizing part of code that takes perhaps 5% or 10% of the whole execution time. The biggest attention should be always given to the parts that need it most.
Even if you do optimize your PHP script, the fact is, simple code optimization does not have that big impact, when it come to creating larger PHP applications. Real reductions in server load are achieved by using proper infrastructures and caches.
Don't get me wrong. There is certainly merit in code speed testing. However, you should be more interested in what solutions are fastest for your problems, and not which functions are generally fastest. For example, whenever I have multiple equally good solutions to a problem, I make decisions based on which code is faster. But I don't make modifications just to squeeze few milliseconds away here and there.
Keep in mind, that I'm of course talking about PHP scripts in general. When you are writing algorithms, the effiency is often much bigger factor, and you do want to make any noticable optimizations, since even small changes can be significant, but this is not something you'll find yourself doing too much with PHP.
Useless optimizations
Labels:
programming,
rant
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment