php echo and print

Is echo really, even negligibly, faster than print in php?

Conventional wisdom of the Internet says “yes”, because print performs one extra op (it returns a value). I’ve seen performance charts with 20% or more difference in performance.

I tested this and for the life of me could not find any significant difference. This is about the simplest test harness anyone can write. I varied the number of tests (typically a few thousand tests) and each test would call echo thousands or millions of times (each with a unique double-quoted string) and the same for print. At reasonable numbers there is no difference, not even a negligible difference:

echo vs print, after 1000000 calls to each
print AVG(/100): 7.0849990844727E-5s
echo  AVG(/100): 7.0495271682739E-5s
 ... that's a 0.50318149504225% difference!

10000 tests were run, each with 100 iterations
------------------------------------------------------------
echo was faster than print on 5193 tests (each with 100 iterations)
print was faster than echo on 4807 tests (each with 100 iterations)

Overall, echo was faster than print, on average, by 3.547191619873E-9 seconds

That’s still one-million calls to echo and one-million calls to print, less than 1% difference, and nearly half the time print was faster than echo. But this is all too reasonable, there must be a difference at an unreasonable number of print and echo calls.

To see even a negligible difference try one-million iterations per test (below is 100 tests at one-million iterations each, for a total of one-hundred-million calls to print and to echo).

echo vs print, after 100000000 calls to each
print AVG(/1000000): 0.77505155324936s
echo  AVG(/1000000): 0.76188613891602s
 ... that's a 1.7280028682599% difference!

100 tests were run, each with 1000000 iterations
------------------------------------------------------------
echo was faster than print on 89 tests (each with 1000000 iterations)
print was faster than echo on 11 tests (each with 1000000 iterations)

Overall, echo was faster than print, on average, by 1.3165414333344E-8 seconds

Finally we start to see at least a negligible difference — although far less than the 20% difference, it’s at least over a single percentile. And 89% of the time echo was indeed faster than print.

That’s a negligible difference we can measure! While we can’t truthfully say echo is faster than print, we can say that iteratively running one-hundred-million echo statements is negligibly faster (most of the time but not always) than iteratively running one-hundred-million print statements.