echo vs print

Submitted by tomek on Sun, 05/08/2011 - 13:58

f2 is slower than f1 by 11.72%

Full source code as executed.
/* How many times the code under test should run in each function */
define('LOOP',10000000);
 
function f1() {
  ob_start();
  for($i=0; $i<LOOP; ++$i) {
    echo 'a';
    echo 'a';
    echo 'a';
    echo 'a';
    echo 'a';
  }
  ob_end_clean();
}
 
function f2() {
  ob_start();
  for($i=0; $i<LOOP; ++$i) {
    print('a');
    print('a');
    print('a');
    print('a');
    print('a');
  }
  ob_end_clean();
}
 
$start = microtime(true);
f1();
$stop = microtime(true);
$time1 = $stop - $start;
 
$start = microtime(true);
f2();
$stop = microtime(true);
$time2 = $stop - $start;
 
echo $time1 . "\t";
echo $time2 . "\n";
Test results
comparisontime1time2php_version
12.45%829236893244395.3.5-1ubuntu7.2
22.08%8473280103443215.3.3-7
13.86%16221453184700955.3.5-1ubuntu7.2
18.04%10555955124598525.3.3-7+squeeze1
12.01%13473721509204php-5.3.6-phpbrew_compiled
49.22%13819152062079php-5.3.7-phpbrew_compiled
6.33%14530311544999php-5.3.8-phpbrew_compiled
9.76%13893391524974php-5.3.9-phpbrew_compiled
9.14%13794091505467php-5.3.10-phpbrew_compiled
9.89%13817151518318php-5.3.11-phpbrew_compiled
10.18%13781961518524php-5.3.12-phpbrew_compiled
9.96%13809221518450php-5.3.13-phpbrew_compiled
2.73%15467491505677php-5.3.14-phpbrew_compiled
9.88%13975301535672php-5.3.15-phpbrew_compiled
10.9%13874881538671php-5.3.16-phpbrew_compiled
28.17%13849321775089php-5.3.17-phpbrew_compiled
17.81%15756371856253php-5.3.18-phpbrew_compiled
0.68%15614951572146php-5.3.19-phpbrew_compiled
7.36%17435461871818php-5.4.0-phpbrew_compiled
8.04%17335881872897php-5.4.1-phpbrew_compiled
7.39%17295381857429php-5.4.2-phpbrew_compiled
8.19%17496871893049php-5.4.3-phpbrew_compiled
7.44%17941281927622php-5.4.4-phpbrew_compiled
8.9%17154361868094php-5.4.5-phpbrew_compiled
11.47%17558281957284php-5.4.6-phpbrew_compiled
6.91%17662541888292php-5.4.7-phpbrew_compiled
8.92%17153451868306php-5.4.8-phpbrew_compiled
5.98%19316522047167php-5.4.9-phpbrew_compiled
50%
updown
50%