PHP echo efficiency

I always use the output variable in PHP, where I collect all the content before I repeat it. Then I read somewhere (I don’t remember where) that you get maximum performance if you split the output variable into packets and then echo each packet instead of the entire output variable.

How is that really?

+3
source share
4 answers

If you output really large lines with an echo, it’s best to use a few echo expressions.

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


Php:
http://bugs.php.net/bug.php?id=18029

+3

:

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

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

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

+3

, ...

http://wonko.com/post/seeing_poor_performance_using_phps_echo_statement_heres_why#comment-5606

........

, , !

, PHP PHP script, "", Apache.

(. ), PHP. , Apache SendBuffer SendBufferSize.

This speeds up data output from PHP. I guess the next step will be to get it out of Apache faster, but I'm not sure if there is another custom level between Apache and the underlying network bandwidth.

0
source

This is my version of the solution, this is echos only if the connection is not interrupted. if the user disconnects, the function exits.

<?php
function _echo(){
    $args    = func_get_args();
    foreach($args as $arg){
        $strs = str_split($arg, 8192);
        foreach($strs as $str){
            if(connection_aborted()){
                break 2;
            }
            echo $str;
        }
    }
}
_echo('this','and that','and more','and even more');
_echo('or just a big long text here, make it as long as you want');
0
source

Source: https://habr.com/ru/post/1709470/


All Articles