PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: foreach vs. for

Note that this isn't helpful unless you have ascending int keys.

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

Result: Discarded

The test labeled "foreach" was the faster by 0.066 seconds, (12.320% faster)

foreach 100%
for 87.68%

The foreach test took 0.4695 seconds.
The for test took 0.5354 seconds.


Each test case ran 20 random code order iterations consisting of 129,674 loops for a total of 2,593,480 runs.

  • Line execution difference (0.000025) milliseconds.
  • Avg difference (3.298) milliseconds per 129,674 loops.
  • Total difference 65.97 milliseconds for 2,593,480 loops

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


The first test, "foreach", was:

foreach ($GLOBALS['dummy'] as $v)
	$v[0] + 1;

The second test, "for", was:

for ($k = 0, $n = count($GLOBALS['dummy']); $k < $n; $k++)
	$GLOBALS['dummy'][$k][0] + 1;

Running: Linux (x86_64:1 GB) PHP (