PHP echo – comma (,) vs dot (.) performance benchmarks

PHP echo allow comma (,) for concatenating various values in addition to dot (.). This works only for echo though. It is sometimes discussed comma may perform better than dot. We will use a php benchmark code to see if there is any significant performance difference.

php-echo-dot-vs-comma-syntax

echo – comma (,) vs dot (.) benchmark environment

  1. OS: Ubuntu 14.04.2 LTS Linux
  2. Machine: Amazon t2.mciro

Benchmark code

Here is the code we’ll use for these benchmarks:

?php
$len = $argv[1];
$loop = $argv[2];
for ($i=0; $i < $len; $i++) {
  $str1 .= "a";
  $str2 .= "b";
  $str3 .= "c";
  $str4 .= "d";
  $str5 .= "e";
}

$t1 = microtime(true);
for ($i=0; $i < $loop; $i++) {
  ob_start();
  echo $str1 . $str2 . $str3 . $str4 . $str5 . "\n";
  ob_end_clean();
}
$t2 = microtime(true);
$diff1 = $t2 - $t1;

for ($i=0; $i < $loop; $i++) {
  ob_start();
  echo $str1 , $str2 , $str3 , $str4, $str5, "\n";
  ob_end_clean();
}
$t3 = microtime(true);
$diff2 = $t3 - $t2;

echo "diff1=$diff1 diff2=$diff2\n";
?>

Benchmark results

Time (s) with dot (.) Time(s) with comma (,)
10 char string, 10M loop iterations 5.95 6.28
100 char string, 10M loop iterations 6.52 6.19

Conclusion

There is no significant difference between the performance of dot vs comma in echo. For smaller strings dot performed little better and for larger strings, comma performed little better. Probably one should not put effort in optimisation on this topic.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments