PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: function vs. class method

Tests for any difference between calling a free function or a member function.

Run this test again Return to test menu

Historical Results

Based on (2) saved test runs, The test labeled "class method" was (on average) the faster by 0.0149 seconds, (1.870% faster)

class method 100%
function 98.13%

The class method test took 0.7845 seconds.
The function test took 0.7994 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 209,533 loops for a total of 4,190,650 runs.

  • Line execution difference (0.000004) milliseconds.
  • Avg difference (0.000) milliseconds per 209,533 loops.
  • Total difference 14.95 milliseconds for 4,190,650 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, "class method", was:

/*
class Example
{
	var $x;

	function foo()
	{
		$this->x = 42;

		return 42;
	}
}
*/

$test = new Example();

for ($i = 0; $i < 100; $i++)
	$GLOBALS['dummy'] = $test->foo();

The second test, "function", was:

/*
function example_foo(&$array)
{
	$array['x'] = 42;

	return 42;
}
*/

$test = array();

for ($i = 0; $i < 100; $i++)
	$GLOBALS['dummy'] = example_foo($test);

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