I have to admit... I'm a bit of a thief. We have an internal blog at work for our developers to use, and I originally wrote the article for that blog -- before I started this blog. But of course, this information is equally useful for you as it is for them. And I wrote it, so whatever :)
16 OpCodes.
Unless you use the proper quoting and interpolation techniques, that's how many instructions it might take to turn your 3 line "Hello World" script into it's intermediate language.
13 Hours.
That's how much time would be spent in a year by a developer who spends "only" 3 minutes each work day refactoring old code (or even new code) to be "more efficient" by optimizing quoted strings, using a different function, etc to reduce opcodes.
A while ago, I passed around an article making fun of micro-optimizations to my co-workers. A few weeks later, a co-worker passed around an article about the inefficiencies of string parsing in PHP. Which article you take to the most probably had only to do with your previous thoughts on your. If you haven't read these two articles, I encourage you to do so before you continue:
I'd like to show you that in the end, it doesn't really matterrrrrrrrrrr (for my Linkin Park fans). Given the article above, you would immediately think that you should never ever use a HEREDOC, because they're so inefficient even if no interpolation happens. But, You might be wrong.
The Test.
I wrote a test which takes a 40 paragraph ( 3727 words, 25409 bytes ) Lorem Ipsum, and assigns it to a varible 10,000 times. The Lorem Ipsum also has 65 seed variables inserted throught it, which would need to be interpolated. I did this for both Single quotes (interpolation using the dot concatenator) and with a HEREDOC (inline interpolation). Who won?
=== CONCAT RESULTS === Single Quotes: 2513.158082962ms Heredoc: 387.84980773926 Difference: 2125.3082752228ms
Surprising? To be honest, it was to me too. Even though the Heredoc uses many many more opcodes to do it's work, in the real world it still performs faster on large datasets. 2 seconds would be significant, if it wasn't for the unrealistic scale of the test. Most of us will be performing that many operations on a dataset that large all in one operation.
Out of curiousity, I wanted to see how single quotes and heredocs matched up face-to-face, without any variable interpolation whatsoever. So I took the same 40 pargraph Lorem Ipsum, and assigned it to a variable 10,000 using single quotes and heredoc syntax.
=== NO CONCAT RESULTS === Single Quotes: 39.492130279541ms Heredoc: 38.956880569458 Difference: 0.53524971008301ms
37 Minutes.
That's how much time I wasted writing the tests and this article. As you can see above, the time you wasted microptimizing would have been better served writing a more efficient database query, or drinking a beer.
I recommend the beer.

No comments:
Post a Comment