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.

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

Result: Discarded

The test labeled "${'array_' . $n}" was the faster by 0.0051 seconds, (0.529% faster)

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

The ${'array_' . $n} test took 0.9655 seconds.
The $array[$n] test took 0.9707 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 220,869 loops for a total of 4,417,380 runs.

  • Line execution difference (0.000001) milliseconds.
  • Avg difference (0.257) milliseconds per 220,869 loops.
  • Total difference 5.14 milliseconds for 4,417,380 loops

The iteration variablity for Code 1 was (5.1325) milliseconds and Code 2 was (4.5230) 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.15-1+ubuntu16.04.1+deb.sury.org+1)