Single quotes VS double quotes, sizeof() VS count(), for VS while… A lot of questions have torn the PHP community appart for decades but one of the most debated question is:
What is fastest? print or echo?!
While I do prefer print for aesthetic reason I'll try to answer this once and for all.
It is always hard to find the correct setup for benchmarking low level langage features, there is always an impact of the script to the actual benchmark so I decided to benchmark directly via my terminal, using php command line and unix time function so that the php script is not polluted by time computing functions to output the time result. Also most of the script will output tons of data, slowing down the rendering in the terminal, so I decided to redirect all output to /dev/null. In the end the benchmark will look like this :
time php script.php > /dev/null
And output something like :
Round 1 - Fight!
The script will be as simple as it can be, just iterating 10 million times the same function, here is echo.php:
time php print.php > /dev/null
All right a little better! print is 0,018 second faster than echo, not much of a difference but that should settle the debate once and for all, unless…
Round 2 - Here comes a new challenger!
And this is when it occured to me… to be honest this thought only is what motivated me to write this post. I've always heard that die() is actually the quickest way to output something, for example <?php die("Hello World!"); would be the fastest ever Hello World you could ever write, but I've never really used die() for that purpose, so lets see what it got!!
time php die.php > /dev/null
ZOMG 0.025 motherfucking seconds! That's blazing fast!! So I think the morale is pretty clear: next time you want to optimize your scripts, please think about dying first. I think you'll save everyone a lot of time!