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

Result: Discarded

The test labeled "sprintf()" was the faster by 0.1299 seconds, (10.865% faster)

sprintf() 100%
pack() 89.135%

The sprintf() test took 1.0657 seconds.
The pack() test took 1.1957 seconds.


Each test case ran 20 random code order iterations consisting of 286,251 loops for a total of 5,725,020 runs.

  • Line execution difference (0.000023) milliseconds.
  • Avg difference (6.495) milliseconds per 286,251 loops.
  • Total difference 129.91 milliseconds for 5,725,020 loops

The iteration variablity for Code 1 was (10.2735) milliseconds and Code 2 was (2.9545) milliseconds. The lower and the closer together there values are the more accurate the results are.


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 (