Skip to content

Instantly share code, notes, and snippets.

@jeremysells
Last active May 9, 2017 23:41
Show Gist options
  • Save jeremysells/6d3ad313664be6baae6c6879dfbcaddc to your computer and use it in GitHub Desktop.
Save jeremysells/6d3ad313664be6baae6c6879dfbcaddc to your computer and use it in GitHub Desktop.
interpolation_vs_concatenation.php
<?php
//This is not just about performance, but also readability!
//But if you can get a win win, why not!
//Please read https://nikic.github.io/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth.html
$varA = "Some really long string here dasdlasmd;mas;lemas;lem;lasmel;asme";
$varB = "Some other string here";
//Test 1
$startTime = microtime(true);
$something = "";
for ($i = 0; $i !== 90000000; $i++) {
$something = "I have {$varA} and {$varB} here";
}
$time = microtime(true) - $startTime;
echo "Time for test one: " . $time . "\n";
unset($startTime, $something, $time, $i);
//Test 2
$startTime = microtime(true);
$something = "";
for ($i = 0; $i !== 90000000; $i++) {
$something = "I have " . $varA . " and " . $varB . " here";
}
$time = microtime(true) - $startTime;
echo "Time for test two: " . $time . "\n";
unset($startTime, $something, $time, $i);
//Test 3
$startTime = microtime(true);
$something = "";
for ($i = 0; $i !== 90000000; $i++) {
$something = 'I have ' . $varA . ' and ' . $varB . ' here';
}
$time = microtime(true) - $startTime;
echo "Time for test three: " . $time . "\n";
//PHP 7.0.15-0 @ 10/05/2017
//Time for test one: 11.101058006287
//Time for test two: 13.977134943008
//Time for test three: 14.210139036179
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment