Skip to content

Instantly share code, notes, and snippets.

@postpersonality
Last active January 28, 2016 14:21
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/d968c4dd4dbd1ae73428 to your computer and use it in GitHub Desktop.
Save postpersonality/d968c4dd4dbd1ae73428 to your computer and use it in GitHub Desktop.
// 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
Copy link
Author

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