Test: in_array vs. key
Is it faster to use a key to lookup (in arrays with many values), or in_array?
View test history (2) Run this test again Return to test menuResult: Discarded
The test labeled "in_array()" was the faster by 0.0204 seconds, (4.127% faster)
The in_array() test took 0.4733 seconds.
The key test took 0.4937 seconds.
Nitty-Gritty
Each test case ran 20 random code order iterations consisting of 134,616 loops for a total of 2,692,320 runs.
- Line execution difference (0.000008) milliseconds.
- Avg difference (1.019) milliseconds per 134,616 loops.
- Total difference 20.37 milliseconds for 2,692,320 loops
The iteration variablity for Code 1 was (0.4884) milliseconds and Code 2 was (1.8229) milliseconds. The lower and the closer together there values are the more accurate the results are.
Code
The first test, "in_array()", was:
for ($i = 0; $i < 10; $i++) $GLOBALS['dummy2'] = in_array($i * 20, $GLOBALS['dummy']);
The second test, "key", was:
for ($i = 0; $i < 10; $i++) $GLOBALS['dummy2'] = isset($GLOBALS['dummy'][$i * 20]);