PHP Benchmarks

Performance comparison of PHP code alternatives.

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 (3) Run this test again Return to test menu

Result: Discarded

The test labeled "key" was the faster by 0.0217 seconds, (2.037% faster)

key 100%
in_array() 97.963%

The key test took 1.0415 seconds.
The in_array() test took 1.0631 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 269,903 loops for a total of 5,398,060 runs.

  • Line execution difference (0.000004) milliseconds.
  • Avg difference (1.083) milliseconds per 269,903 loops.
  • Total difference 21.66 milliseconds for 5,398,060 loops

The iteration variablity for Code 1 was (3.7307) milliseconds and Code 2 was (1.7396) 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]);

Running: Linux (x86_64:1 GB) PHP (7.2.15-1+ubuntu16.04.1+deb.sury.org+1)