Skip to content

Instantly share code, notes, and snippets.

@postpersonality
Last active January 28, 2016 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save postpersonality/9d52e81a37d6763b35b0 to your computer and use it in GitHub Desktop.
Save postpersonality/9d52e81a37d6763b35b0 to your computer and use it in GitHub Desktop.
// 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;
@postpersonality
Copy link
Author

Benchmark runs: 20000000 interpolation: 23.426885843277 concatenation: 26.726758956909
Benchmark runs: 20000000 interpolation: 23.434737920761 concatenation: 26.155627012253

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment