Last active
October 5, 2015 22:28
-
-
Save snpy/2888189 to your computer and use it in GitHub Desktop.
isset() vs in_array() - super simple test - no breaks
This file contains hidden or 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 | |
| define('MAX', 100000); | |
| define('NUM', 100); | |
| $in = array('key'); | |
| for ($i = 0; $i < NUM; ++$i) { | |
| $in100[] = 'key' . $i; | |
| } | |
| $is = array('key' => true,); | |
| for ($i = 0; $i < NUM; ++$i) { | |
| $is100['key' . $i] = true; | |
| } | |
| $mt = microtime(1); | |
| for ($i = 0; $i < MAX; ++$i) { | |
| isset($is['key']); | |
| } | |
| printf('Small isset() exists: %e', microtime(1) - $mt); | |
| echo PHP_EOL; | |
| $mt = microtime(1); | |
| for ($i = 0; $i < MAX; ++$i) { | |
| isset($is['key0']); | |
| } | |
| printf('Small isset() not exists: %e', microtime(1) - $mt); | |
| echo PHP_EOL; | |
| $mt = microtime(1); | |
| for ($i = 0; $i < MAX; ++$i) { | |
| in_array('key', $in); | |
| } | |
| printf('Small in_array() exists: %e', microtime(1) - $mt); | |
| echo PHP_EOL; | |
| $mt = microtime(1); | |
| for ($i = 0; $i < MAX; ++$i) { | |
| in_array('key0', $in); | |
| } | |
| printf('Small in_array() not exists: %e', microtime(1) - $mt); | |
| echo PHP_EOL; | |
| $mt = microtime(1); | |
| for ($i = 0; $i < MAX; ++$i) { | |
| isset($is100['key1']); | |
| } | |
| printf('Big isset() exists near start: %e', microtime(1) - $mt); | |
| echo PHP_EOL; | |
| $mt = microtime(1); | |
| for ($i = 0; $i < MAX; ++$i) { | |
| isset($is100['key98']); | |
| } | |
| printf('Big isset() exists near end: %e', microtime(1) - $mt); | |
| echo PHP_EOL; | |
| $mt = microtime(1); | |
| for ($i = 0; $i < MAX; ++$i) { | |
| isset($is100['key']); | |
| } | |
| printf('Big isset() not exists near end: %e', microtime(1) - $mt); | |
| echo PHP_EOL; | |
| $mt = microtime(1); | |
| for ($i = 0; $i < MAX; ++$i) { | |
| in_array('key1', $in100); | |
| } | |
| printf('Big in_array() exists near start: %e', microtime(1) - $mt); | |
| echo PHP_EOL; | |
| $mt = microtime(1); | |
| for ($i = 0; $i < MAX; ++$i) { | |
| in_array('key98', $in100); | |
| } | |
| printf('Big in_array() exists near end: %e', microtime(1) - $mt); | |
| echo PHP_EOL; | |
| $mt = microtime(1); | |
| for ($i = 0; $i < MAX; ++$i) { | |
| in_array('key', $in100); | |
| } | |
| printf('Big in_array() not exists near end: %e', microtime(1) - $mt); | |
| echo PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment