Argument was:
Argument was invalid.
PHP string formatting benchmark

I'm working on some PHP right now, and I want it as fast as possible. Naturally, I have to output some interesting stuff; concatenated strings, with integer or floating-point values in them.

The question becomes: is it faster to print values that must be formatted, such as the string "\n", or integers or floating-point numbers, by:

  1. Including them in one long double-quoted string
  2. Concatenating them between multiple single-quoted strings
  3. Using the heredoc format
  4. Using the sprintf() function

All calculations are done by putting the value into a variable, outstr. Once the string is obtained, a simple print or echo can be used to output it. This way I don't actually have to generate a lot of output in the HTML, so I can run the test online.

calculating a very long string using only single-quote concatenation... 1773181136406ms.
calculating the same string using a large double-quoted concatenation... 1773181136406ms.
calculating the same string using a heredoc... 1773181136406ms.
calculating the same string using an sprintf()... 1773181136407ms.
calculating the same string using an sprintf() with positional params... 1773181136407ms.

Graphical comparisons

Method (1000 repetitions)Percent of time out of max (1773181136407 ms)
Single quote concat
Double quote eval
Heredoc eval
Simple sprintf()
sprintf() with positional params