Skip to content

Instantly share code, notes, and snippets.

@klapuch
Created January 30, 2018 14:14
Show Gist options
  • Save klapuch/37d4497859358eae0b24679725811861 to your computer and use it in GitHub Desktop.
Save klapuch/37d4497859358eae0b24679725811861 to your computer and use it in GitHub Desktop.
<?php
$iterations = 4000000;
$a = 'A';
$b = 'B';
$c = 'C';
$d = 'D';
$e = 10;
$f = 30;
$start = microtime(true);
for($i = 0; $i < $iterations; ++$i) {
$output = <<<EOT
A $a B $b C $c D $d E $e F $f
EOT;
}
$end = microtime(true);
echo 'Heredoc: ' . ($end - $start) . PHP_EOL;
$start = microtime(true);
for($i = 0; $i < $iterations; ++$i) {
$output = "A $a B $b C $c D $d E $e F $f";
}
$end = microtime(true);
echo 'Concat via double quotes: ' . ($end - $start) . PHP_EOL;
$start = microtime(true);
for($i = 0; $i < $iterations; ++$i) {
$output = sprintf('A %s B %s C %s D %s E %d F %d', $a, $b, $c, $d, $e, $f);
}
$end = microtime(true);
echo 'Sprintf: ' . ($end - $start) . PHP_EOL;
$start = microtime(true);
for($i = 0; $i < $iterations; ++$i) {
$output = 'A ' . $a . ' B ' . $b . ' C ' . $c . ' D ' . $d . ' E ' . $e . ' F ' . $f;
}
$end = microtime(true);
echo 'Concat: ' . ($end - $start) . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment