Created
August 5, 2022 17:48
-
-
Save colshrapnel/17b54f56e2ae06890a317b817f0b727d to your computer and use it in GitHub Desktop.
Measuring array_column+array_search vs. foreach
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 | |
| $size = 10000; | |
| $a = []; | |
| for ($i = 0; $i < $size; $i++) { | |
| $a[] = ['uid' => $i, 'name' => 'Sandra Shush'.$i, 'pic_square' => 'urlof'.$i]; | |
| } | |
| shuffle($a); | |
| $count = 1000; | |
| $t = microtime(1); | |
| for ($i = 0; $i < $count; $i++) { | |
| $needle = rand(0, $size-1); | |
| foreach ($a as $key => $val) { | |
| if ($val['uid'] === $needle) { | |
| $id = $key; | |
| break; | |
| } | |
| } | |
| } | |
| echo "loop: ".(round(microtime(1)-$t,4))."\n"; | |
| $t = microtime(1); | |
| for ($i = 0; $i < $count; $i++) { | |
| $needle = rand(0, $size-1); | |
| $id = array_search($needle, array_column($a, 'uid')); | |
| } | |
| echo "sugar: ".(round(microtime(1)-$t,4))."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment