Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP echo performance

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?

like image 510
Ivar Avatar asked Jan 02 '26 06:01

Ivar


2 Answers

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

like image 78
Silfverstrom Avatar answered Jan 03 '26 18:01

Silfverstrom


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

like image 29
karim79 Avatar answered Jan 03 '26 20:01

karim79



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!