PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: passing classes vs. arrays

Is there any penalty (either way) in passing class objects vs. arrays? Theoretically, they are semantically equivalent.

Run this test again Return to test menu

Historical Results

Based on (2) saved test runs, The test labeled "classes" was (on average) the faster by 0.0077 seconds, (0.731% faster)

classes 100%
arrays 99.269%

The classes test took 1.0409 seconds.
The arrays test took 1.0485 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 290,894 loops for a total of 5,817,870 runs.

  • Line execution difference (0.000001) milliseconds.
  • Avg difference (0.000) milliseconds per 290,894 loops.
  • Total difference 7.67 milliseconds for 5,817,870 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, "classes", was:

/*
class Example
{
	var $x;
}

function example_class($x)
{
	$GLOBALS['dummy'] = $x->x;
}

$GLOBALS['test_object'] = new Example();
$GLOBALS['test_object']->x = 42;
*/

example_class($GLOBALS['test_object']);

The second test, "arrays", was:

/*
function example_array($x)
{
	$GLOBALS['dummy'] = $x['x'];
}

$GLOBALS['test_array'] = array();
$GLOBALS['test_array']['x'] = 42;
*/

example_array($GLOBALS['test_array']);

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