PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: array_map vs. array_walk

array_walk() can change an array in place. Is this faster than creating a separate one with array_map()?

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

Result: Discarded

The test labeled "array_walk()" was the faster by 0.0332 seconds, (3.163% faster)

array_walk() 100%
array_map() 96.837%

The array_walk() test took 1.0158 seconds.
The array_map() test took 1.0489 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 279,853 loops for a total of 5,597,060 runs.

  • Line execution difference (0.000006) milliseconds.
  • Avg difference (1.659) milliseconds per 279,853 loops.
  • Total difference 33.18 milliseconds for 5,597,060 loops

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

Code

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

/*
function foo_map($v)
{
	return $v + 1;
}
*/

$GLOBALS['dummy'] = array_map('foo_map', $GLOBALS['dummy']);

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

/*
function foo_walk(&$v)
{
	$v = $v + 1;
}
*/

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

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