Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Benchmark params
$arrLen = 100000;
$missRate = 10;
$runs = 10000000;
// Gen test array
$array = [];
for ($i = 0; $i < $arrLen; $i++) {
$array[$arrLen * $missRate + rand(0, $missRate)] = true;
}
echo 'Benchmark runs: ' . $runs . PHP_EOL;
// Benchmark isset
$timeStart = microtime(true);
$counter = 0;
for ($i = 0; $i < $runs; $i++) {
$counter += isset($array[rand(0, $arrLen * $missRate)]);
}
echo 'isset: ' . (microtime(true) - $timeStart) . PHP_EOL;
// Benchmark array_key_exist
$timeStart = microtime(true);
for ($i = 0; $i < $runs; $i++) {
$counter += array_key_exists(rand(0, $arrLen * $missRate), $array);
}
echo 'array_key_exists: ' . (microtime(true) - $timeStart) . PHP_EOL;
@postpersonality

This comment has been minimized.

Copy link
Owner Author

commented Jan 28, 2016

Benchmark runs: 10000000 isset: 6.4032361507416 array_key_exists: 11.386907815933
Benchmark runs: 10000000 isset: 6.5448701381683 array_key_exists: 11.398876905441

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.