Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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

This comment has been minimized.

Copy link
Owner Author

commented Jan 28, 2016

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
You can’t perform that action at this time.