PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: single class vs. associative arrays

This test creates objects or arrays, and fills them with equivalent data. The array is prefilled with nulls in attempt to be fair.

Run this test again Return to test menu

Result: Discarded

The test labeled "arrays" was the faster by 0.0417 seconds, (3.800% faster)

arrays 100%
class 96.2%

The arrays test took 1.0566 seconds.
The class test took 1.0984 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 297,468 loops for a total of 5,949,360 runs.

  • Line execution difference (0.000007) milliseconds.
  • Avg difference (2.087) milliseconds per 297,468 loops.
  • Total difference 41.74 milliseconds for 5,949,360 loops

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

Code

The first test, "class", was:

/*
class TestClass
{
	var $x, $y, $z;
}
*/

$data = new TestClass();

$data->x = 'test';
$data->y = 1;
$data->z = 5.5;

The second test, "arrays", was:

$data = array(
	'x' => null,
	'y' => null,
	'z' => null,
);

$data['x'] = 'test';
$data['y'] = 1;
$data['z'] = 5.5;

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