Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Benchmarking PHP text output

You asked for it, you got it



5:26 pm on Mar 21, 2004 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Since this keeps coming up, I thought I would post this. I've hesitated in the past since it's such a huge block of text, but here it is. There are four tests here.

Test 1. Assign simple text strings to variables. No output.
Test 2. Assign text strings with variables to variables. No output.
Test 3. Output strings with variables, using fastest method from Test 2.
Test 4. Output huge blocks of text.

Tests all include three parts
1. Description of the test
2. Results
3. Interpretation

- All results sorted from slowest to fastest, so first is worst.
- Results are given in seconds
- Times are those returned from XDebug profiler
- I ran most of these tests several times to verify that these results are representative. To the extent that they vary with multiple tests, that is noted in the "interpretation" section.

For Tests 1 and 2, the function names tell how the text is being assigned.

heredoc_* use heredoc
accum_* use .= on each line
concat_* use concatenate with . to begin each line and one single equals
*_sq_* use single quotes
*_dq_* use double quotes
*_sub_* use variable substitution within the string
*_concat_* use concatenation within the string

Test 1: assigning simple strings.
- no variable substition.
- 20 lines of 60 characters of plain text assigned to a variable with a function
- 100 iterations within each function
- each function called one time.

Sample Results
0.0231628418 *heredoc_no_vars
0.0031390190 *accum_sq_no_vars
0.0030751228 *accum_dq_no_vars
0.0028319359 *concat_sq_no_vars
0.0027120113 *concat_dq_no_vars

- heredoc is an order of magnitude slower than other methods.
- sq is slightly faster than dq
- concatenation is the fastest

Test2: assigning strings with variables in them
- same functions: 20 lines, 60chars, 100 iterations
- each function called 5 times
- each line has the same string substituted at three points
- string changes with each function call.
- times are average of five function calls.

Sample Results

0.0348237038 *accum_dq_var_sub
0.0313258171 *concat_dq_var_sub
0.0267303944 *heredoc_var_sub
0.0254049778 *concat_sq_var_concat
0.0245302677 *concat_dq_var_concat
0.0137676239 *accum_sq_var_concat
0.0134190083 *accum_dq_var_concat

*There's more variation with this one and accum_dq_var_sub and concat_dq_var_sub trade places for slowest.
*The two fastest also trade places.
*Obviously double-quote variable substitution makes the processor work hard and concatenation roughly 3x faster than using

variable substitution.

Test 3: echoing strings with variables

- since accum_dq_var_concat was fastest, we use that as the base.

- same output, but this time we try four possibilities
1. echo each line and use concatenation to construct the line
2+3. switch to html and back to php each time there is a variable to output, using shorthand (i.e. <?=$var;?>) and echo (<?echo $var;?>).
4. accumulate strings to a variable and then echo $var at the end (thus only one echo statement).

- call each function 15 times. Each function prints the 20 lines of text 100 times

Sample Results

0.2290725390 *escape_php_shorthand
0.2152340094 *escape_php_echo
0.2005011082 *echo_concatenated_lines
0.0146572272 *accum_then_echo

* Lots of variability here. Any of the three slowest methods can change places and are usually close to each other (+ or - 20%).
* accumulating all output to a variable and then echoing once is way faster. Always at least an order of magnitude.

Test 4: huge chunks of text, no variables
- this basically tests how much extra overhead is incurred by running everything through PHP.
- five iterations outputting about a 2MB string


**Assign string to var then echo var
Opcode Compiling: 24.6104161739
Total Execution: 7.4180238247
Total Processing: 32.0284399986

**Leave php on each iteration
Opcode Compiling: 0.2842299938
Total Execution: 29.7363541126
Total Processing: 30.0205841064

**echo <<<heredoc string
Opcode Compiling: 2.4830060005
Total Execution: 27.4329669476
Total Processing: 29.9159729481


Only 10% variation and this is about the best possible case for exiting php. The advantages of stepping out of php seem to be minimal for large strings and the disadvantages substantial for shorter strings. Therefore, you never lost much time by echoing, but you can by stepping in and out of PHP.


7:09 pm on Mar 21, 2004 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

I'm surprised by the heredoc report. After all, it's supposed to "behave just like a double-quoted string, without the double-quotes." Thanks for taking the time to test and share.


7:25 pm on Mar 21, 2004 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

nice ergophobe.

Those are about in line with test that I have run in the past, though I don't remember heredoc being so slow but I also never use it.

I often run tests within specific parts of my applications to see if the same general timings remain true and foten combine various methods depending on readability and speed.

Nice to know though that if you use whatever is most comfortable for you there aren't any massive time implications.


8:26 pm on Mar 21, 2004 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Actually I found surprises with each test, not just the heredoc result in test 1.

Test 1.
- Surprising how bad the performance of heredoc is when not doing any variable substitution.

Test 2.
- Surprising how much less variation there is when putting variables in your strings.
- Surprising that heredoc outperforms simple substitution here.
- Surprising that it is 3x faster (though small on an absolute scale) if you switch from variable substitution to "accumulation" (.=) to begin each new line and concatenation ("string " . $var . " string").

Test 3
- accumulating all output to a variable and then echoing once is always at least an order of magnitude faster than echoing piecemeal or switching in and out of PHP.

Test 4
- even with huge strings (2mb) with no php processing, there is such a small advantage to switching out of php.


Featured Threads

Hot Threads This Week

Hot Threads This Month