PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: arrays vs. variables

Not that using variables like this is good style, and array1 already mostly answers this.

Run this test again Return to test menu

Result: Discarded

The test labeled "$array[$n]" was the faster by 0.1445 seconds, (11.056% faster)

$array[$n] 100%
${'array_' . $n} 88.944%

The $array[$n] test took 1.1622 seconds.
The ${'array_' . $n} test took 1.3067 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 275,442 loops for a total of 5,508,840 runs.

  • Line execution difference (0.000026) milliseconds.
  • Avg difference (7.224) milliseconds per 275,442 loops.
  • Total difference 144.47 milliseconds for 5,508,840 loops

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

Code

The first test, "$array[$n]", was:

$array = array();
for ($i = 0; $i < 100; $i++)
	$array[$i] = $i;

The second test, "${'array_' . $n}", was:

for ($i = 0; $i < 100; $i++)
	${'array_' . $i} = $i;

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