Test: (string) vs. concat
Concatenating to a string always results in a string, but isn't a cast faster?
Run this test again Return to test menuResult: Discarded
The test labeled "(string)" was the faster by 0.0076 seconds, (1.691% faster)
The (string) test took 0.4428 seconds.
The $string . '' test took 0.4505 seconds.
Nitty-Gritty
Each test case ran 20 random code order iterations consisting of 127,467 loops for a total of 2,549,340 runs.
- Line execution difference (0.000003) milliseconds.
- Avg difference (0.381) milliseconds per 127,467 loops.
- Total difference 7.62 milliseconds for 2,549,340 loops
The iteration variablity for Code 1 was (0.2540) milliseconds and Code 2 was (0.5112) milliseconds. The lower and the closer together there values are the more accurate the results are.
Code
The first test, "(string)", was:
for ($j = 0; $j < 1000; $j++) $GLOBALS['dummy'] = (string) $j;
The second test, "$string . ''", was:
for ($j = 0; $j < 1000; $j++) $GLOBALS['dummy'] = $j . '';