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?

Run this test again Return to test menu

Historical Results

Based on (1) saved test runs, The test labeled "in_array()" was (on average) the faster by 0.0146 seconds, (1.377% faster)

in_array() 100%
key 98.623%

The in_array() test took 1.0448 seconds.
The key test took 1.0594 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 292,693 loops for a total of 5,853,860 runs.

  • Line execution difference (0.000002) milliseconds.
  • Avg difference (0.000) milliseconds per 292,693 loops.
  • Total difference 14.59 milliseconds for 5,853,860 loops

The iteration variablity for Code 1 was (0.0000) milliseconds and Code 2 was (0.0000) 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.34-18+ubuntu16.04.1+deb.sury.org+1)