PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: array_push vs. [] =

According to the documentation, these are equivalent. Past tests have shown otherwise...

Run this test again Return to test menu

Historical Results

Based on (2) saved test runs, The test labeled "array_push()" was (on average) the faster by 0.0162 seconds, (2.063% faster)

array_push() 100%
$array[] = 97.937%

The array_push() test took 0.7667 seconds.
The $array[] = test took 0.7829 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 212,259 loops for a total of 4,245,180 runs.

  • Line execution difference (0.000004) milliseconds.
  • Avg difference (0.000) milliseconds per 212,259 loops.
  • Total difference 16.15 milliseconds for 4,245,180 loops

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

Code

The first test, "array_push()", was:

$array = array();
for ($n = 0; $n < 8; $n++)
{
	for ($i = 0; $i < 50; $i++)
		array_push($array, $i);
}

The second test, "$array[] =", was:

$array = array();
for ($n = 0; $n < 8; $n++)
{
	for ($i = 0; $i < 50; $i++)
		$array[] = $i;
}

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