PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: array_walk vs. array_walk_recursive

Does the recursive variant have any benefit over rolling our own recursion?

Run this test again Return to test menu

Result: Discarded

The test labeled "array_walk()" was the faster by 0.0251 seconds, (2.310% faster)

array_walk() 100%
array_walk_recursive() 97.69%

The array_walk() test took 1.0605 seconds.
The array_walk_recursive() test took 1.0856 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 210,214 loops for a total of 4,204,280 runs.

  • Line execution difference (0.000006) milliseconds.
  • Avg difference (1.254) milliseconds per 210,214 loops.
  • Total difference 25.08 milliseconds for 4,204,280 loops

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

Code

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

/*
function foo_manual(&$v)
{
	if (is_array($v))
		array_walk($v, 'foo_manual');
	else
		$v++;
}
*/

array_walk($GLOBALS['dummy'], 'foo_manual');

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

/*
function foo_auto(&$v)
{
	$v++;
}
*/

array_walk_recursive($GLOBALS['dummy'], 'foo_auto');

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