<?php

// testing the speed of echo vs print
// a script by Ted Percival (midg3t)
// date: april 2004
// this document is free for distribution... GPL or something.

// so you don't have to run it, echo took about 0.9 times as long as print on my machine after several tests.
// that means echo is 10% faster :)

error_reporting(E_ALL);

ob_start();

// use each function to nullify any overhead associated with the function first being called (unlikely)
echo 'init';
print 
'init';
microtime();

// initialise all variables
$estime '';
$eetime '';

$pstime '';
$petime '';

///////////////////////////////////////////////////////////////

// IMPORTANT: These first two are just dummy runs so that PHP will allocate all the memory it needs before the real benchmarking.
// without the dummy runs (at least 1), we get wildly inaccurate results because of all the memory allocation.

///////////////////////////////////////////////////////////////

// now for print

$i 1;

$pstime microtime();
for (
$i 0$i 500000; ++$i) {
    print(
'foo');
}
$petime microtime();
ob_clean();


/////////////////////////////////////////////////////////////
$i 1;

$estime microtime();
for (
$i 0$i 500000; ++$i) {
    echo(
'foo');
}
$eetime microtime();

ob_clean();

///////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

// now for the REAL benchmarks :)
// you can swap these two around without any noticable difference in speed.

/////////////////////////////////////////////////////////////
$i 1;

sleep(1); // so there's time to start fresh... maybe swap PHP out of the CPU for a moment or something

$estime microtime();
for (
$i 0$i 500000; ++$i) {
    echo(
'foo');
}
$eetime microtime();

ob_clean();


/////////////////////////////////////////////////////////////////

// now for print

$i 1;

sleep(1); // so there's time to start fresh... maybe swap PHP out of the CPU for a moment or something

$pstime microtime();
for (
$i 0$i 500000; ++$i) {
    print(
'foo');
}
$petime microtime();
ob_clean();


////////////////////////////////////////////////////////////////

ob_end_clean();


// now calculate time differences.
$estimes explode(' '$estime);
$eetimes explode(' '$eetime);

$pstimes explode(' '$pstime);
$petimes explode(' '$petime);

$edif = ($eetimes[0] + $eetimes[1]) - ($estimes[0] + $estimes[1]);
$pdif = ($petimes[0] + $petimes[1]) - ($pstimes[0] + $pstimes[1]);

$ratio $edif $pdif;

echo 
"$edif << echo\n";
echo 
"$pdif << print\n";
echo 
"\n";
echo 
"edif to pdif ratio is $ratio\n";

echo 
"Using php version " phpversion() . "\n";

?>