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

Result: Discarded

The test labeled "array_keys(array_flip())" was the faster by 0.0046 seconds, (0.444% faster)

array_keys(array_flip()) 100%
array_unique() 99.556%

The array_keys(array_flip()) test took 1.0407 seconds.
The array_unique() test took 1.0453 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 262,062 loops for a total of 5,241,240 runs.

  • Line execution difference (0.000001) milliseconds.
  • Avg difference (0.232) milliseconds per 262,062 loops.
  • Total difference 4.64 milliseconds for 5,241,240 loops

The iteration variablity for Code 1 was (2.9994) milliseconds and Code 2 was (1.3322) 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();
for ($n = 0; $n < 8; $n++)
{
	for ($i = 0; $i < 50; $i++)
		$array[] = $i;
}

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

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

$array = array();
for ($n = 0; $n < 8; $n++)
{
	for ($i = 0; $i < 50; $i++)
		$array[] = $i;
}

$array = array_unique($array);

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