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

Result: Discarded

The test labeled "key" was the faster by 0.0149 seconds, (2.045% faster)

key 100%
in_array() 97.955%

The key test took 0.7136 seconds.
The in_array() test took 0.7285 seconds.


Each test case ran 20 random code order iterations consisting of 150,037 loops for a total of 3,000,740 runs.

  • Line execution difference (0.000005) milliseconds.
  • Avg difference (0.745) milliseconds per 150,037 loops.
  • Total difference 14.90 milliseconds for 3,000,740 loops

The iteration variablity for Code 1 was (6.7740) milliseconds and Code 2 was (7.4341) milliseconds. The lower and the closer together there values are the more accurate the results are.


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 (