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

Result: Saved

The test labeled "!empty()" was the faster by 0.004 seconds, (0.817% faster)

!empty() 100%
(bool) 99.183%

The !empty() test took 0.4841 seconds.
The (bool) test took 0.4881 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 124,266 loops for a total of 2,485,320 runs.

  • Line execution difference (0.000002) milliseconds.
  • Avg difference (0.199) milliseconds per 124,266 loops.
  • Total difference 3.99 milliseconds for 2,485,320 loops

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