PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: array_keys(array_flip) vs. array_unique

Do note that this test is only using integer values (which is common for this), long strings will take longer to hash as array keys.

View test history (1) Run this test again Return to test menu

Result: Discarded

The test labeled "array_unique()" was the faster by 0.0276 seconds, (2.115% faster)

array_unique() 100%
array_keys(array_flip()) 97.885%

The array_unique() test took 1.2756 seconds.
The array_keys(array_flip()) test took 1.3032 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 280,696 loops for a total of 5,613,920 runs.

  • Line execution difference (0.000005) milliseconds.
  • Avg difference (1.378) milliseconds per 280,696 loops.
  • Total difference 27.57 milliseconds for 5,613,920 loops

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

Code

The first test, "array_keys(array_flip())", was:

$array = array(1,2,3,4,5,6,7,8,9);
$a = 1;

if (in_array(1,$array) || $a)
	$b = true;

$array = array_keys(array_flip($array));

The second test, "array_unique()", was:

$array = array(1,2,3,4,5,6,7,8,9);
$a = 1;

if ($a || in_array(1,$array))
	$b = true;

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