Last active
January 28, 2016 14:17
-
-
Save postpersonality/9d52e81a37d6763b35b0 to your computer and use it in GitHub Desktop.
This file contains 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
// Benchmark params | |
$runs = 10000000; | |
// Gen test array | |
$testStr = 'string'; | |
$testInt = -100; | |
$testFloat = .1; | |
$testArr = [$testStr, 'key' => $testInt]; | |
$testObj = new \StdClass(); | |
$testObj->{0} = $testStr; | |
$testObj->key = $testInt; | |
echo 'Benchmark runs: ' . $runs . PHP_EOL; | |
// Benchmark isset | |
$timeStart = microtime(true); | |
for ($i = 0; $i < $runs; $i++) { | |
$str = "test $testStr $testInt $testFloat $testArr[0] {$testArr['key']} {$testObj->{0}} {$testObj->key} end"; | |
} | |
echo 'interpolation: ' . (microtime(true) - $timeStart) . PHP_EOL; | |
// Benchmark array_key_exist | |
$timeStart = microtime(true); | |
for ($i = 0; $i < $runs; $i++) { | |
$str = 'test ' . $testStr . ' ' . $testInt . ' ' . $testFloat . ' ' . $testArr[0] . ' ' . $testArr['key'] . ' ' . $testObj->{0} . ' ' . $testObj->key . ' end'; | |
} | |
echo 'concatenation: ' . (microtime(true) - $timeStart) . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Benchmark runs: 20000000 interpolation: 23.426885843277 concatenation: 26.726758956909
Benchmark runs: 20000000 interpolation: 23.434737920761 concatenation: 26.155627012253