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?

Run this test again Return to test menu

Historical Results

Based on (1) saved test runs, The test labeled "!empty()" was (on average) the faster by 0.0067 seconds, (1.249% faster)

!empty() 100%
(bool) 98.751%

The !empty() test took 0.5287 seconds.
The (bool) test took 0.5354 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 125,427 loops for a total of 2,508,540 runs.

  • Line execution difference (0.000003) milliseconds.
  • Avg difference (0.000) milliseconds per 125,427 loops.
  • Total difference 6.69 milliseconds for 2,508,540 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, "(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)