echo vs printf

kgruszczynski's picture
Submitted by kgruszczynski on Sat, 05/21/2011 - 19:38

f2 is slower than f1 by 39.95%

Full source code as executed.
/* How many times the code under test should run in each function */
define('LOOP',10000000);
 
function f1() {
  for($i=0; $i<LOOP; ++$i) {
    ob_start();
    echo "test";
    ob_end_clean();
  }
}
function f2() {
  for($i=0; $i<LOOP; ++$i) {
   ob_start();
   printf("test");
   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
50.35%18740248281752955.3.5-1ubuntu7.2
65.55%21398962354260175.3.3-7
57.17%724463091138664165.3.5-1ubuntu7.2
63.05%21154307344931405.3.3-7+squeeze1
43.74%27636653972361php-5.3.6-phpbrew_compiled
42.93%28082544013856php-5.3.7-phpbrew_compiled
35.56%29978184063915php-5.3.8-phpbrew_compiled
40.39%28702404029645php-5.3.9-phpbrew_compiled
48.15%27793394117623php-5.3.10-phpbrew_compiled
40.2%28196393953015php-5.3.11-phpbrew_compiled
40.25%28293763968236php-5.3.12-phpbrew_compiled
40.38%28213133960517php-5.3.13-phpbrew_compiled
45.67%27995234078013php-5.3.14-phpbrew_compiled
41.98%28187214001979php-5.3.15-phpbrew_compiled
41.77%29120094128476php-5.3.16-phpbrew_compiled
43.74%28169744049014php-5.3.17-phpbrew_compiled
45.68%30873064497451php-5.3.18-phpbrew_compiled
35.17%29568523996635php-5.3.19-phpbrew_compiled
28.75%33163564269910php-5.4.0-phpbrew_compiled
29.22%33352574309824php-5.4.1-phpbrew_compiled
33.95%33344054466380php-5.4.2-phpbrew_compiled
29.86%33896554401835php-5.4.3-phpbrew_compiled
33.3%33121964415293php-5.4.4-phpbrew_compiled
33.3%33122394415122php-5.4.5-phpbrew_compiled
28.42%33116204252849php-5.4.6-phpbrew_compiled
28.57%33142054261181php-5.4.7-phpbrew_compiled
25.13%36196474529106php-5.4.8-phpbrew_compiled
26.47%34686754386873php-5.4.9-phpbrew_compiled
100%
updown
0%