PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: min/max vs. if

No Description

Run this test again Return to test menu

Historical Results

Based on (1) saved test runs, The test labeled "min()/max()" was (on average) the faster by 0.0069 seconds, (1.415% faster)

min()/max() 100%
ifs 98.585%

The min()/max() test took 0.4817 seconds.
The ifs test took 0.4886 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 135,365 loops for a total of 2,707,300 runs.

  • Line execution difference (0.000003) milliseconds.
  • Avg difference (0.000) milliseconds per 135,365 loops.
  • Total difference 6.92 milliseconds for 2,707,300 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, "min()/max()", was:

$GLOBALS['dummy'] = 83;
for ($j = 0; $j < 100; $j++)
	$GLOBALS['dummy2'] = min(50, max(20, $GLOBALS['dummy']));

The second test, "ifs", was:

$GLOBALS['dummy'] = 83;
for ($j = 0; $j < 100; $j++)
{
	if ($GLOBALS['dummy'] < 20)
		$GLOBALS['dummy2'] = 20;
	elseif ($GLOBALS['dummy'] > 50)
		$GLOBALS['dummy2'] = 50;
	else
		$GLOBALS['dummy2'] = $GLOBALS['dummy'];
}

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