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.0124 seconds, (2.332% faster)
The (bool) test took 0.5204 seconds.
The !empty() test took 0.5328 seconds.
Nitty-Gritty
Each test case ran 20 random code order iterations consisting of 145,056 loops for a total of 2,901,120 runs.
- Line execution difference (0.000004) milliseconds.
- Avg difference (0.621) milliseconds per 145,056 loops.
- Total difference 12.43 milliseconds for 2,901,120 loops
The iteration variablity for Code 1 was (0.9416) milliseconds and Code 2 was (1.5364) 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); }