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_push()" was the faster by 0.038 seconds, (3.254% faster)

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

The array_push() test took 1.129 seconds.
The $array[] = test took 1.1669 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 284,890 loops for a total of 5,697,800 runs.

  • Line execution difference (0.000007) milliseconds.
  • Avg difference (1.899) milliseconds per 284,890 loops.
  • Total difference 37.97 milliseconds for 5,697,800 loops

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