Test: substr($var, 5, 1) vs. isset && {5}
Return the nth character of a string, substring vs string array checking.
Run this test again Return to test menuResult: Discarded
The test labeled "substr()" was the faster by 0.0546 seconds, (8.220% faster)
The substr() test took 0.6091 seconds.
The isset {5} test took 0.6637 seconds.
Nitty-Gritty
Each test case ran 20 random code order iterations consisting of 159,297 loops for a total of 3,185,940 runs.
- Line execution difference (0.000017) milliseconds.
- Avg difference (2.728) milliseconds per 159,297 loops.
- Total difference 54.55 milliseconds for 3,185,940 loops
The iteration variablity for Code 1 was (2.2839) milliseconds and Code 2 was (5.7157) milliseconds. The lower and the closer together there values are the more accurate the results are.
Code
The first test, "substr()", was:
for ($j = 0; $j < 100; $j++) $GLOBALS['dummy2'] = substr($GLOBALS['dummy'], 5, 1);
The second test, "isset {5}", was:
for ($j = 0; $j < 100; $j++) $GLOBALS['dummy2'] = isset($GLOBALS['dummy']{5}) ? $GLOBALS['dummy']{5} : '';