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.

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

Result: Discarded

The test labeled "classes" was the faster by 0.0751 seconds, (8.438% faster)

classes 100%
arrays 91.562%

The classes test took 0.8146 seconds.
The arrays test took 0.8896 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 156,899 loops for a total of 3,137,980 runs.

  • Line execution difference (0.000024) milliseconds.
  • Avg difference (3.753) milliseconds per 156,899 loops.
  • Total difference 75.07 milliseconds for 3,137,980 loops

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