echo all the time vs echo at the end - what is faster

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • windandwaves

    echo all the time vs echo at the end - what is faster

    Hi Folk

    My question is:
    echo all the time vs echo at the end - what is faster

    Let me explain

    I used to write pages like this:

    echo "<head> ";
    {code code code}
    echo "<body>";
    {code code code, talk to the database, etc...}
    echo "<div>some more shite</div>";
    {code code code}
    echo "</html>";


    Now I do this:

    $v .= "<head> ";
    {code code code}
    $v .= "<body>";
    {code code code, talk to the database, etc...}
    $v .= "<div>some more shite</div>";
    {code code code}
    $v .= "</html>";
    echo $v;

    The advantage of the second method is that I can still manipulate the
    content before it goes out. for example, I can replace double spaces and
    tabs, alter menus, etc... using simple replace functions.

    Now, my question is, what is faster or better? I kind of like the latter
    option, but I do not want to use it if it makes my website (significantly)
    slower.

    Cheers


    Nicolaas


  • Colin McKinnon

    #2
    Re: echo all the time vs echo at the end - what is faster

    windandwaves wrote:
    [color=blue]
    > echo "<head> ";
    > {code code code}
    > echo "<body>";
    > {code code code, talk to the database, etc...}
    > echo "<div>some more shite</div>";
    > {code code code}
    > echo "</html>";
    >
    >
    > Now I do this:
    >
    > $v .= "<head> ";
    > {code code code}
    > $v .= "<body>";
    > {code code code, talk to the database, etc...}
    > $v .= "<div>some more shite</div>";
    > {code code code}
    > $v .= "</html>";
    > echo $v;
    >
    > The advantage of the second method is that I can still manipulate the
    > content before it goes out. for example, I can replace double spaces and
    > tabs, alter menus, etc... using simple replace functions.
    >
    > Now, my question is, what is faster or better? I kind of like the latter
    > option, but I do not want to use it if it makes my website (significantly)
    > slower.
    >[/color]

    I think you've answered your own question as to which is better. Regards the
    question of speed - why don't you measure it. I suspect you'll find the
    latter is faster because it won't use chunked encoding - particularly if
    you are using compression.

    A better way of buffering the output is to use PHP's output buffering
    functions.

    C.[color=blue]
    > Cheers
    >
    >
    > Nicolaas[/color]

    Comment

    • Justin Koivisto

      #3
      Re: echo all the time vs echo at the end - what is faster

      windandwaves wrote:[color=blue]
      > Hi Folk
      >
      > My question is:
      > echo all the time vs echo at the end - what is faster
      >
      > Let me explain
      >
      > I used to write pages like this:
      >
      > echo "<head> ";
      > {code code code}
      > echo "<body>";
      > {code code code, talk to the database, etc...}
      > echo "<div>some more shite</div>";
      > {code code code}
      > echo "</html>";
      >
      >
      > Now I do this:
      >
      > $v .= "<head> ";
      > {code code code}
      > $v .= "<body>";
      > {code code code, talk to the database, etc...}
      > $v .= "<div>some more shite</div>";
      > {code code code}
      > $v .= "</html>";
      > echo $v;
      >
      > The advantage of the second method is that I can still manipulate the
      > content before it goes out. for example, I can replace double spaces and
      > tabs, alter menus, etc... using simple replace functions.
      >
      > Now, my question is, what is faster or better? I kind of like the latter
      > option, but I do not want to use it if it makes my website (significantly)
      > slower.[/color]

      Consider this as a third option:
      <?php
      ob_start();
      echo '<html>';
      // other stuff here
      echo '</html>';

      $v=ob_get_clean ();
      // do stuff
      echo $v;
      ?>

      By creating a large string with what you have as your second method, you
      continuously concatenate to the end of a growing string. Concatenation
      takes longer than calling a language construct like "echo" to output
      contents. There have been quite a few discussions here about the use of
      single or double quotes as well as the use of heredoc syntax and the
      performance of each.

      In the end, it's a matter of preference. You can always get better
      server hardware relatively inexpensively if you feel your code it taking
      too long to execute. You could also purchase profiling software that
      could help you find bottlenecks in your code. The method(s) you use to
      output the results of your script are almost never the bottle neck, and
      rarely improve performance in a noticeable manner.

      --
      Justin Koivisto, ZCE - justin@koivi.co m

      Comment

      • Chung Leong

        #4
        Re: echo all the time vs echo at the end - what is faster

        A recent survey shows that 4 out of 5 dentists prefer the first method.

        Comment

        • windandwaves

          #5
          Re: echo all the time vs echo at the end - what is faster

          Justin Koivisto wrote:

          ....
          [color=blue]
          > The
          > method(s) you use to output the results of your script are almost
          > never the bottle neck, and rarely improve performance in a noticeable
          > manner.[/color]

          Hi Justin,

          Thank you for your reply. You are right, the speed of the script is not
          really the problem. I was more thinking about the connection between server
          and client. That is, is it faster to send one chunk a few "seconds" later
          OR does it make more sense to send small chunks immediately OR are all the
          small chunks congregated anyway on the way to the client and so it makes
          absolutely no difference?

          I hear what you saying about adding to a long string, that can take some
          time.

          Testing is not really an option, it would take too long and I work on so
          many different servers that it could become a complete project in itself ;-)

          Thanks again

          - Nicolaas


          Comment

          • windandwaves

            #6
            Re: echo all the time vs echo at the end - what is faster

            Colin McKinnon wrote:
            ....[color=blue]
            > A better way of buffering the output is to use PHP's output buffering
            > functions.[/color]

            but that means that I can not manipulate the output anymore? right?


            Comment

            • Sandman

              #7
              Re: echo all the time vs echo at the end - what is faster

              In article <mrPaf.768$xD6. 46316@news.xtra .co.nz>,
              "windandwav es" <winandwaves@co ldmail.com> wrote:
              [color=blue]
              > Hi Folk
              >
              > My question is:
              > echo all the time vs echo at the end - what is faster
              >
              > Let me explain
              >
              > I used to write pages like this:
              >
              > echo "<head> ";
              > {code code code}
              > echo "<body>";
              > {code code code, talk to the database, etc...}
              > echo "<div>some more shite</div>";
              > {code code code}
              > echo "</html>";
              >
              >
              > Now I do this:
              >
              > $v .= "<head> ";
              > {code code code}
              > $v .= "<body>";
              > {code code code, talk to the database, etc...}
              > $v .= "<div>some more shite</div>";
              > {code code code}
              > $v .= "</html>";
              > echo $v;
              >
              > The advantage of the second method is that I can still manipulate the
              > content before it goes out. for example, I can replace double spaces and
              > tabs, alter menus, etc... using simple replace functions.
              >
              > Now, my question is, what is faster or better? I kind of like the latter
              > option, but I do not want to use it if it makes my website (significantly)
              > slower.[/color]

              <?
              ob_start();
              ?>
              HTML Code and stuff
              <?
              $output = ob_get_clean();
              print $output;
              ?>

              I.e. no need to rewrite all your stuff.


              --
              Sandman[.net]

              Comment

              • Malcolm Dew-Jones

                #8
                Re: echo all the time vs echo at the end - what is faster

                windandwaves (winandwaves@co ldmail.com) wrote:
                : Colin McKinnon wrote:
                : ...
                : > A better way of buffering the output is to use PHP's output buffering
                : > functions.

                : but that means that I can not manipulate the output anymore? right?

                Wrong.

                After doing the buffering, one of the ob_ functions can return the output
                to your program as a string, which you can then manipulate to your hearts
                content.


                --

                This programmer available for rent.

                Comment

                • R. Rajesh Jeba Anbiah

                  #9
                  Re: echo all the time vs echo at the end - what is faster

                  windandwaves wrote:[color=blue]
                  > Hi Folk
                  >
                  > My question is:
                  > echo all the time vs echo at the end - what is faster[/color]
                  <snip>

                  As everyone stated, comparing the two, (1) is faster. But, none of
                  them are faster as compared to embedded HTML/PHP.
                  <quote src="http://in2.php.net/language.basic-syntax#AEN2666" >
                  for outputting large blocks of text, dropping out of PHP parsing mode
                  is generally more efficient than sending all of the text through echo()
                  or print().
                  </quote>


                  --
                  <?php echo 'Just another PHP saint'; ?>
                  Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

                  Comment

                  • windandwaves

                    #10
                    Re: echo all the time vs echo at the end - what is faster

                    R. Rajesh Jeba Anbiah wrote:[color=blue]
                    > windandwaves wrote:[color=green]
                    >> Hi Folk
                    >>
                    >> My question is:
                    >> echo all the time vs echo at the end - what is faster[/color]
                    > <snip>
                    >
                    > As everyone stated, comparing the two, (1) is faster. But, none of
                    > them are faster as compared to embedded HTML/PHP.
                    > <quote src="http://in2.php.net/language.basic-syntax#AEN2666" >
                    > for outputting large blocks of text, dropping out of PHP parsing mode
                    > is generally more efficient than sending all of the text through
                    > echo() or print().
                    > </quote>[/color]

                    Agree. While speed is important to me, efficient code is probably even more
                    important to me.

                    I now use the "ob_start and flush" method to clean up all my html (it
                    becomes one line without any tabs, double spaces) for speed, while keeping
                    the code nicely embedded in php (much easier to put in variables without the
                    labourious <?php echo $my_variable; ?> every time you want to do so).

                    Thanks

                    - Nicolaas


                    Comment

                    Working...