Last active
January 28, 2016 14:21
-
-
Save postpersonality/d968c4dd4dbd1ae73428 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 | |
$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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Benchmark runs: 10000000 isset: 6.4032361507416 array_key_exists: 11.386907815933
Benchmark runs: 10000000 isset: 6.5448701381683 array_key_exists: 11.398876905441