views:

310

answers:

2

I'm always using an output variable in PHP where I gather all the content before I echo it. Then I read somewhere (I don't remember where, though) that you get best performance if you split the output variable into packets and then echo each packet instead of the whole output variable.

How is it really?

+2  A: 

If you are outputting really big strings with echo, it is better to use multiple echo statements.

This is because of the way Nagle's algorithm causes data to be buffered over TCP/IP.


Found an note on Php-bugs about it:
http://bugs.php.net/bug.php?id=18029

Silfverstrom
So you recommend to split the output variable specified on a maximum size and echo each piece?
Ivarska
If you have strong reasons to believe it will increase your performance, yes i do.
Silfverstrom
Well, I mentioned my reason in my first post - putting all the content to a single variable might be a reason strong enough.
Ivarska
I don't think Nagle's Algorithm would apply here. The HTML isn't sent to the client until the server completes rendering it. The size of the package(s) being transmitted over TCI/IP would not change.
Babak Naffas
+3  A: 

This will automatically break up big strings into smaller chunks and echo them out:

function echobig($string, $bufferSize = 8192) {
  $splitString = str_split($string, $bufferSize);

  foreach($splitString as $chunk) {
    echo $chunk;
  }
}

Source: http://wonko.com/post/seeing_poor_performance_using_phps_echo_statement_heres_why

karim79
Oh, that's the site I readed it from. Thank you.
Ivarska