PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: foreach/range vs. for

No Description

Run this test again Return to test menu

Result: Discarded

The test labeled "foreach" was the faster by 0.1155 seconds, (8.662% faster)

foreach 100%
for 91.338%

The foreach test took 1.218 seconds.
The for test took 1.3335 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 291,625 loops for a total of 5,832,500 runs.

  • Line execution difference (0.000020) milliseconds.
  • Avg difference (5.775) milliseconds per 291,625 loops.
  • Total difference 115.51 milliseconds for 5,832,500 loops

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

Code

The first test, "foreach", was:

$range = range(0, 100);
foreach ($range as $n)
	$GLOBALS['dummy'] = $n;

The second test, "for", was:

for ($n = 0; $n <= 100; $n++)
	$GLOBALS['dummy'] = $n;

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