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 menuResult: Discarded
The test labeled "(bool)" was the faster by 0.1202 seconds, (9.466% faster)
The (bool) test took 1.1497 seconds.
The !empty() test took 1.2699 seconds.
Nitty-Gritty
Each test case ran 20 random code order iterations consisting of 268,478 loops for a total of 5,369,560 runs.
- Line execution difference (0.000022) milliseconds.
- Avg difference (6.010) milliseconds per 268,478 loops.
- Total difference 120.21 milliseconds for 5,369,560 loops
The iteration variablity for Code 1 was (10.6981) milliseconds and Code 2 was (14.7743) 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); }