PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: (bool) vs. !empty

According to the documentation, empty is like isset and a reverse bool cast. Perhaps together, those are faster than the cost of the ! on empty?

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

Result: Discarded

The test labeled "(bool)" was the faster by 0.0312 seconds, (3.243% faster)

(bool) 100%
!empty() 96.757%

The (bool) test took 0.9315 seconds.
The !empty() test took 0.9627 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 252,783 loops for a total of 5,055,660 runs.

  • Line execution difference (0.000006) milliseconds.
  • Avg difference (1.561) milliseconds per 252,783 loops.
  • Total difference 31.22 milliseconds for 5,055,660 loops

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

Code

The first test, "(bool)", was:

for ($j = 0; $j < 1000; $j++)
{
	$i = '1';
	$GLOBALS['dummy'] = isset($i) && (bool) $i;
}

The second test, "!empty()", was:

for ($j = 0; $j < 1000; $j++)
{
	$i = '1';
	$GLOBALS['dummy'] = !empty($i);
}

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