String concatenation vs commas

Submitted by tomek on Sun, 05/08/2011 - 14:00

f2 is slower than f1 by 10.62%

Full source code as executed.
define('LOOP',1000000);
function f1() {
  ob_start();
  for($i=0; $i<LOOP; ++$i) {
    echo '123','456';
    echo '123','456';
    echo '123','456';
    echo '123','456';
    echo '123','456';
  }
  ob_end_clean();
}
 
function f2() {
  ob_start();
  for($i=0; $i<LOOP; ++$i) {
    echo '123'.'456';
    echo '123'.'456';
    echo '123'.'456';
    echo '123'.'456';
    echo '123'.'456';
  }
  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
30.35%135241017628285.3.5-1ubuntu7.2
37.74%143933219825425.3.3-7
25.05%282069335274095.3.5-1ubuntu7.2
22.57%178785021913935.3.3-7+squeeze1
13.99%223770255084php-5.3.6-phpbrew_compiled
20.51%228546275414php-5.3.7-phpbrew_compiled
13.02%239390270555php-5.3.8-phpbrew_compiled
14.42%229419262500php-5.3.9-phpbrew_compiled
20.26%226817272764php-5.3.10-phpbrew_compiled
15.43%225877260738php-5.3.11-phpbrew_compiled
15.64%226146261522php-5.3.12-phpbrew_compiled
15.2%226029260389php-5.3.13-phpbrew_compiled
8.34%247388268026php-5.3.14-phpbrew_compiled
17.9%229106270116php-5.3.15-phpbrew_compiled
17.66%228035268314php-5.3.16-phpbrew_compiled
16.14%230130267272php-5.3.17-phpbrew_compiled
13.88%249864284554php-5.3.18-phpbrew_compiled
12.73%246937278379php-5.3.19-phpbrew_compiled
4.1%334305321132php-5.4.0-phpbrew_compiled
3.38%325480314845php-5.4.1-phpbrew_compiled
0.95%325859322785php-5.4.2-phpbrew_compiled
3.47%330245319176php-5.4.3-phpbrew_compiled
4.27%334594320903php-5.4.4-phpbrew_compiled
3.07%323726314094php-5.4.5-phpbrew_compiled
2.68%329874321251php-5.4.6-phpbrew_compiled
5.11%331839315719php-5.4.7-phpbrew_compiled
1.87%322587316679php-5.4.8-phpbrew_compiled
4.55%347850332717php-5.4.9-phpbrew_compiled
50%
updown
50%