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 (2) Run this test again Return to test menu

Result: Discarded

The test labeled "(bool)" was the faster by 0.0372 seconds, (4.904% faster)

(bool) 100%
!empty() 95.096%

The (bool) test took 0.721 seconds.
The !empty() test took 0.7582 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 175,255 loops for a total of 3,505,100 runs.

  • Line execution difference (0.000011) milliseconds.
  • Avg difference (1.859) milliseconds per 175,255 loops.
  • Total difference 37.18 milliseconds for 3,505,100 loops

The iteration variablity for Code 1 was (1.7051) milliseconds and Code 2 was (2.1517) 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.15-1+ubuntu16.04.1+deb.sury.org+1)