PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: array_diff vs. ==

Is using array_diff() better than an == check?

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

Result: Discarded

The test labeled "==" was the faster by 0.0633 seconds, (7.866% faster)

== 100%
array_diff() 92.134%

The == test took 0.7413 seconds.
The array_diff() test took 0.8046 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 190,045 loops for a total of 3,800,900 runs.

  • Line execution difference (0.000017) milliseconds.
  • Avg difference (3.165) milliseconds per 190,045 loops.
  • Total difference 63.29 milliseconds for 3,800,900 loops

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

Code

The first test, "array_diff()", was:

for ($n = 0; $n < 20; $n++)
	$GLOBALS['dummy2'] = count(array_diff($GLOBALS['dummy'], $GLOBALS['dummy_pass'], $GLOBALS['dummy_pass2'])) === 0;
for ($n = 0; $n < 20; $n++)
	$GLOBALS['dummy2'] = count(array_diff($GLOBALS['dummy'], $GLOBALS['dummy_fail'], $GLOBALS['dummy_fail2'])) === 0;

The second test, "==", was:

for ($n = 0; $n < 20; $n++)
	$GLOBALS['dummy2'] = $GLOBALS['dummy'] == $GLOBALS['dummy_pass'] && $GLOBALS['dummy'] == $GLOBALS['dummy_pass2'];
for ($n = 0; $n < 20; $n++)
	$GLOBALS['dummy2'] = $GLOBALS['dummy'] == $GLOBALS['dummy_fail'] && $GLOBALS['dummy'] == $GLOBALS['dummy_fail2'];

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