Wednesday, November 7, 2012

PHP echo vs print vs printf Performance test / Benchmark / Alternatives

Hi, All
Today we going to test the performance of php's functions: echo versus print versus printf. The test stand as usual, my workstation:)
The test will run 1 million of outputs.

Test Stand:
Hardware:
Intel i7 860 @ 2.8Ghz
Asus P7H55-M PRO
8GB DDR @ 1600Mhz Timings 8-8-8-24
SSD 80GB Intel SA2M080

Software
Windows 7 x64
Apache 2.2.17 (x86)
PHP 5.3.5

Let's begin.

Our test script looks such simple as:
set_time_limit(0);
$time_start = microtime(true);

$i=0;
while($i < 1000000){
    // initial command, whether echo, print of print_r
    ++$i;
}

echo number_format(microtime(true)-$time_start, 6, '.', ''),' seconds \n';

PHP Echo vs Print vs Printf Benchmark results:

* Note: Remember, tests timings are calculated as average based on 10 runs.
Output of string '1' single quoted and double quoted:
echo : Single quotes: 0.111sec. (Double quotes: 0.112sec. )
print : Single quotes: 0.120sec. (Double quotes: 0.120sec. )
printf : Single quotes: 0.468sec. (Double quotes: 0.468sec. )
In the case of sting w/o concatenation, variables, etc. the echo is fastest, while printf is slowest. Also as we can notice the double quotes makes a little difference in execution times. Lets proceed to a bit more complicated benchmarks.

This time we will output 1 character stored in variable for 1 million times, let's see whether we will have the difference.
echo : 0.117sec.
print : 0.128sec.
printf : 0.400sec.
The execution time are growing up, as expected, but printf shown increased performance towards the competitors. Anyways print_r stays slowest option of three.

Case: 'string'.$static_var.'string'.
*Note: static var i assume is 'z', not $i that changes every loop as in next test case.
echo : Single quoted: 0.282sec (Double quoted: 0.261sec )
print : Single quoted: 0.298sec (Double quoted: 0.274sec )
printf : Single quoted: 0.605sec (Double quoted: 0.547sec )
Seems very interesting:) No matter which scenario, the echo is fastest yet.

Next test case will perform string concatenation of three string in a way 'string'.var.'string' (using dots), the var is dynamic variable $i.
In case of printf used %s.
echo : single quoted: 0.481sec. (Double quoted: 0.483sec)
print : single quoted: 0.492sec. (Double quoted: 0.492sec)
printf : single quoted: 0.879sec (Double quoted: 0.879sec)
Conclusion: The echo is fastest yet, while printf is slowest. let's proceed testing other methods.

Next test case is output with commas. ',' rather than concatenating.
Static variable, assumes not changes every loop. Always the same. echo : Single quoted: 0.216sec (Double quoted: 0.243sec)
print : DOESN'T SUPPORT
printf : Single quoted: 0.534sec (Double quoted: 0.572sec)

PHP echo vs. print vs. printf performance/benchmark conclusions:
The conclusion is anyhow the echo is always faster, so preferred way to output data, also you may notice that using single quotes makes the execution a little, but faster. So take it to your coding standard and go with echo and single quotes.

Sincerely,
Valentin Ruskevych

No comments: