Created
January 27, 2016 22:51
-
-
Save aurimasniekis/adb6b826089c08d9c380 to your computer and use it in GitHub Desktop.
PHP Checking if array key exists and getting value
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
<?php | |
$given = array_flip(range(1, 99999)); | |
$start = microtime(true); | |
for ($i = 0; $i < 100000; $i++) { | |
if (false !== isset($given['3434'])) { | |
$value = $given['3434']; | |
} | |
} | |
echo "First: " . (microtime(true) - $start) . PHP_EOL; | |
$start = microtime(true); | |
for ($i = 0; $i < 10000; $i++) { | |
$value = $given['8912'] ?? null; | |
if (null !== $value) { | |
} | |
} | |
echo "Second: " . (microtime(true) - $start) . PHP_EOL; |
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
First: 0.02646803855896 | |
Second: 0.0024199485778809 |
getbyid
commented
May 7, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment