PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: tail recurison

Is using tail-end recursion (where the recursion happens at the end of the function) any better?

Run this test again Return to test menu

Historical Results

Based on (1) saved test runs, The test labeled "normal recursion" was (on average) the faster by 0.0167 seconds, (1.651% faster)

normal recursion 100%
tail recursion 98.349%

The normal recursion test took 0.9944 seconds.
The tail recursion test took 1.0111 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 277,263 loops for a total of 5,545,260 runs.

  • Line execution difference (0.000003) milliseconds.
  • Avg difference (0.000) milliseconds per 277,263 loops.
  • Total difference 16.69 milliseconds for 5,545,260 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, "normal recursion", was:

/*
function recurse_tail($i, $inside)
{
	$data = '<div>' . $inside . '</div>';

	if ($i <= 0)
		return $data;
	else
		return recurse_tail($i - 1, $data);
}
*/

$result = recurse_tail(200, '');

The second test, "tail recursion", was:

/*
function recurse_normal($i, $inside)
{
	$data = '<div>';
	if ($i <= 0)
		$data .= $inside;
	else
		$data .= recurse_normal($i - 1, $data);
	$data .= '</div>';

	return $data;
}
*/

$result = recurse_normal(200, '');

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