Created
October 14, 2015 10:34
-
-
Save wolffc/0dde6505841949b9a9be to your computer and use it in GitHub Desktop.
Performance test of String Concattination in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$testruns = 10000000; | |
$controllruns = 3; | |
$var = 12; | |
$var2 = 'bar'; | |
$var3 = 'foo'; | |
echo 'PHP Version: ' . phpversion() . PHP_EOL; | |
echo 'Operating System: ' . php_uname() . PHP_EOL; | |
for ($j = 0; $j < $controllruns; $j++){ | |
echo PHP_EOL . '---------------' . PHP_EOL; | |
// Single Quotes | |
$start = microtime(true); | |
for ($i = 0; $i < $testruns; $i++){ | |
$string = 'This is a Test: var:' . $var . ' var2:' . $var2 . ' var3:' . $var3; | |
} | |
$end = microtime(true); | |
echo 'single Quote ' . number_format($end - $start, 3) . PHP_EOL; | |
// Double Quotes Quotes | |
$start = microtime(true); | |
for ($i = 0; $i < $testruns; $i++){ | |
$string = "This is a Test: var:$var var2:$var2 var3:$var3"; | |
} | |
$end = microtime(true); | |
echo 'Dobule Quotes Quote ' . number_format($end - $start, 3) . PHP_EOL; | |
// Curly Quotes Quotes + curly Braces | |
$start = microtime(true); | |
for ($i = 0; $i < $testruns; $i++){ | |
$string = "This is a Test: var:{$var} var2:{$var2} var3:{$var3}"; | |
} | |
$end = microtime(true); | |
echo 'Dobule Quotes with curly Braces ' . number_format($end - $start, 3) . PHP_EOL; | |
// sprintf | |
$start = microtime(true); | |
for ($i = 0; $i < $testruns; $i++){ | |
$string = sprintf('This is a Test: var:%d var2:%s var3:%s', $var, $var2, $var3); | |
} | |
$end = microtime(true); | |
echo 'sprintf ' . number_format($end - $start, 3) . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment