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.

View test history (1) Run this test again Return to test menu

Result: Discarded

The test labeled "class" was the faster by 0.0244 seconds, (3.013% faster)

class 100%
arrays 96.987%

The class test took 0.7868 seconds.
The arrays test took 0.8112 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 188,064 loops for a total of 3,761,280 runs.

  • Line execution difference (0.000006) milliseconds.
  • Avg difference (1.222) milliseconds per 188,064 loops.
  • Total difference 24.44 milliseconds for 3,761,280 loops

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