PHP Benchmarks

Performance comparison of PHP code alternatives.

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 menu

Historical Results

Based on (1) saved test runs, The test labeled "pack()" was (on average) the faster by 0.0108 seconds, (1.140% faster)

pack() 100%
sprintf() 98.86%

The pack() test took 0.9378 seconds.
The sprintf() test took 0.9486 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 263,378 loops for a total of 5,267,560 runs.

  • Line execution difference (0.000002) milliseconds.
  • Avg difference (0.000) milliseconds per 263,378 loops.
  • Total difference 10.82 milliseconds for 5,267,560 loops

The iteration variablity for Code 1 was (0.0000) milliseconds and Code 2 was (0.0000) 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);

Running: Linux (x86_64:1 GB) PHP (7.2.15-1+ubuntu16.04.1+deb.sury.org+1)