PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: set immediately or later

Is it better to put values inside array() when filling an array or to put values into it later?

Run this test again Return to test menu

Historical Results

Based on (3) saved test runs, The test labeled "immediate" was (on average) the faster by 0.0096 seconds, (1.415% faster)

immediate 100%
later 98.585%

The immediate test took 0.6688 seconds.
The later test took 0.6784 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 157,379 loops for a total of 3,147,587 runs.

  • Line execution difference (0.000003) milliseconds.
  • Avg difference (0.000) milliseconds per 157,379 loops.
  • Total difference 9.60 milliseconds for 3,147,587 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, "immediate", was:

for ($i = 0; $i < 10; $i++)
	$GLOBALS['dummy2'] = array(
		0 => 'hello',
		1 => 'good bye',
		2 => 'thank you',
		3 => 'sorry',
	);

The second test, "later", was:

for ($i = 0; $i < 10; $i++)
{
	$GLOBALS['dummy2'] = array();
	$GLOBALS['dummy2'][0] = 'hello';
	$GLOBALS['dummy2'][1] = 'good bye';
	$GLOBALS['dummy2'][2] = 'thank you';
	$GLOBALS['dummy2'][3] = 'sorry';
}

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