PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: recurison vs. not (trivial)

What is the cost of a recursive algorithm vs. a non-recursive one?

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

Result: Discarded

The test labeled "non-recursive" was the faster by 0.0324 seconds, (3.660% faster)

non-recursive 100%
recursive 96.34%

The non-recursive test took 0.8518 seconds.
The recursive test took 0.8842 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 226,964 loops for a total of 4,539,280 runs.

  • Line execution difference (0.000007) milliseconds.
  • Avg difference (1.618) milliseconds per 226,964 loops.
  • Total difference 32.36 milliseconds for 4,539,280 loops

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

Code

The first test, "non-recursive", was:

/*
function recurse_not($i)
{
	$result = 0;

	while ($i > 0)
	{
		$result += $i;
		$i -= 5;
	}

	return $result;
}
*/

$result = recurse_not(5000);

The second test, "recursive", was:

/*
function recurse_normal($i)
{
	if ($i > 0)
		return $i + recurse_normal($i - 5);
	else
		return 0;
}
*/

$result = recurse_normal(5000);

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