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

Result: Discarded

The test labeled "$array[] =" was the faster by 0.059 seconds, (4.996% faster)

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

The $array[] = test took 1.1225 seconds.
The array_push() test took 1.1815 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 290,363 loops for a total of 5,807,260 runs.

  • Line execution difference (0.000010) milliseconds.
  • Avg difference (2.951) milliseconds per 290,363 loops.
  • Total difference 59.03 milliseconds for 5,807,260 loops

The iteration variablity for Code 1 was (7.8146) milliseconds and Code 2 was (6.9820) 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.34-21+ubuntu16.04.1+deb.sury.org+1)