Test: pack v. sprintf
The documentation describes sprintf() using %c as similar to chr(). It's an arguably cleaner syntax (if only using chars) than pack() uses - is it faster?
Run this test again Return to test menuResult: Discarded
The test labeled "pack()" was the faster by 0.0077 seconds, (1.251% faster)
The pack() test took 0.6071 seconds.
The sprintf() test took 0.6148 seconds.
Nitty-Gritty
Each test case ran 20 random code order iterations consisting of 128,138 loops for a total of 2,562,760 runs.
- Line execution difference (0.000003) milliseconds.
- Avg difference (0.385) milliseconds per 128,138 loops.
- Total difference 7.69 milliseconds for 2,562,760 loops
The iteration variablity for Code 1 was (7.6025) milliseconds and Code 2 was (8.0954) milliseconds. The lower and the closer together there values are the more accurate the results are.
Code
The first test, "pack()", was:
$GLOBALS['dummy'] = pack('C4', 65, 65, 65, 65);
The second test, "sprintf()", was:
$GLOBALS['dummy'] = sprintf('%c%c%c%c', 65, 65, 65, 65);