PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: foreach (fcall) vs. foreach ($temp)

Is the array being iterated cached? It could cause some nasty side effects to OOP if it is, but maybe PHP is smart about this.

Run this test again Return to test menu

Result: Discarded

The test labeled "foreach ($temp)" was the faster by 0.0471 seconds, (3.708% faster)

foreach ($temp) 100%
foreach (fcall) 96.292%

The foreach ($temp) test took 1.2228 seconds.
The foreach (fcall) test took 1.2699 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 275,307 loops for a total of 5,506,140 runs.

  • Line execution difference (0.000009) milliseconds.
  • Avg difference (2.355) milliseconds per 275,307 loops.
  • Total difference 47.09 milliseconds for 5,506,140 loops

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

Code

The first test, "foreach (fcall)", was:

foreach (array_merge(range(1, 10), range(11, 20), range(21, 30), range(31, 40)) as $v)
	$GLOBALS['dummy2'] = $v;

The second test, "foreach ($temp)", was:

$temp = array_merge(range(1, 10), range(11, 20), range(21, 30), range(31, 40));
foreach ($temp as $v)
	$GLOBALS['dummy2'] = $v;

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