Performance of lots of ECHO's V Jumping In/Out of PHP

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

    Performance of lots of ECHO's V Jumping In/Out of PHP

    Hi,
    Suppose you have the situation where you have say 20 blocks of text (~250
    chars each) in a MySQL db that you need to display, but each has a condition
    to check to see whether you should display it or not.

    As far as I can see, you have 2 options:

    1. A continuous block of php, with all the text displayed via echo commands

    2. Or jumping in and out of php for each block of text i.e.
    <?php ...if condition ?>
    pure html
    <?php ...if end ?>

    Which would be more performant? I understand that echo's are poor in
    performance with large blocks of text, but what is the overhead of the web
    server (Apache) making repeated calls to the php engine?

    My general point is, is it "better" to echo html out (css, formatting and
    all) or continuously jump in/out of php (and only echo MySQL data) if you
    have a lot of php logic / control in your page?

    Thanks,
    M


  • David T. Ashley

    #2
    Re: Performance of lots of ECHO's V Jumping In/Out of PHP

    "M" <nospam@here.th ankyouwrote in message
    news:NjG4h.4964 7$r61.43856@tex t.news.blueyond er.co.uk...
    >
    Which would be more performant? I understand that echo's are poor in
    performance with large blocks of text, but what is the overhead of the web
    server (Apache) making repeated calls to the php engine?
    It is possible that even the authors of PHP/Zend don't know for sure.
    Real-time analysis of software is complex. It might be easiest for you to
    construct a test.

    To eliminate the effects of network variability, you could use "curl" and
    "time" directly on the remote machine with Apache and PHP.

    Dave.



    Comment

    • ZeldorBlat

      #3
      Re: Performance of lots of ECHO's V Jumping In/Out of PHP


      David T. Ashley wrote:
      "M" <nospam@here.th ankyouwrote in message
      news:NjG4h.4964 7$r61.43856@tex t.news.blueyond er.co.uk...

      Which would be more performant? I understand that echo's are poor in
      performance with large blocks of text, but what is the overhead of the web
      server (Apache) making repeated calls to the php engine?
      >
      It's also worth noting that Apache only makes one call to the PHP
      engine. In other words, the entire script is parsed and executed by
      PHP -- there is no "jumping in and out" just because the ?tag is
      encountered.

      Comment

      • M

        #4
        Re: Performance of lots of ECHO's V Jumping In/Out of PHP


        "ZeldorBlat " <zeldorblat@gma il.comwrote in message
        news:1163082078 .036743.251700@ e3g2000cwe.goog legroups.com...
        >
        David T. Ashley wrote:
        >"M" <nospam@here.th ankyouwrote in message
        >news:NjG4h.496 47$r61.43856@te xt.news.blueyon der.co.uk...
        >
        Which would be more performant? I understand that echo's are poor in
        performance with large blocks of text, but what is the overhead of the
        web
        server (Apache) making repeated calls to the php engine?
        >>
        >
        It's also worth noting that Apache only makes one call to the PHP
        engine. In other words, the entire script is parsed and executed by
        PHP -- there is no "jumping in and out" just because the ?tag is
        encountered.
        >
        So in that case - PHP engine scans the whole page of text, acting only on
        code it finds between the php start / end tags?
        I guess then I am wondering whether it is more efficient for the PHP engine
        to start / stop it's processing of php code multiple times or whether it is
        more efficient to output ECHO statements between the if conditions? Think
        that I'll put a test page together to test this.

        As a side question / answer - what would peoples preference be in this case
        from a view point of understanding / reading / maintaining code be?

        M.


        Comment

        • David T. Ashley

          #5
          Re: Performance of lots of ECHO's V Jumping In/Out of PHP

          "M" <nospam@here.th ankyouwrote in message
          news:qiH4h.4967 3$r61.46796@tex t.news.blueyond er.co.uk...
          >
          So in that case - PHP engine scans the whole page of text, acting only on
          code it finds between the php start / end tags?
          You have to understand that PHP and/or the Zend scripting engine is going to
          be designed using formal lexical analysis theory (Lex, Yacc, that kind of
          thing), and will be blindingly fast. You'll probably find that you can't
          measure a speed difference between echo'd HTML and inline HTML. "echo" is
          probably a built-in operator handled mostly by the parser (i.e. not a
          function call with a formal interface like most functions). It may be
          slightly slower, but any difference may be very hard to measure.

          In any case, let us know what you find.
          As a side question / answer - what would peoples preference be in this
          case from a view point of understanding / reading / maintaining code be?
          That depends on why "echo" is used.

          If it is used for invariant HTML that could just be put inline, then
          definitely the inline HTML is easier for people to deal with. IMHO.



          Comment

          • Tim Hunt

            #6
            Re: Performance of lots of ECHO's V Jumping In/Out of PHP


            M wrote:
            "ZeldorBlat " <zeldorblat@gma il.comwrote in message
            news:1163082078 .036743.251700@ e3g2000cwe.goog legroups.com...

            David T. Ashley wrote:
            "M" <nospam@here.th ankyouwrote in message
            news:NjG4h.4964 7$r61.43856@tex t.news.blueyond er.co.uk...

            Which would be more performant? I understand that echo's are poor in
            performance with large blocks of text, but what is the overhead of the
            web
            server (Apache) making repeated calls to the php engine?
            >
            It's also worth noting that Apache only makes one call to the PHP
            engine. In other words, the entire script is parsed and executed by
            PHP -- there is no "jumping in and out" just because the ?tag is
            encountered.
            >
            So in that case - PHP engine scans the whole page of text, acting only on
            code it finds between the php start / end tags?
            I guess then I am wondering whether it is more efficient for the PHP engine
            to start / stop it's processing of php code multiple times or whether it is
            more efficient to output ECHO statements between the if conditions? Think
            that I'll put a test page together to test this.
            >
            As a side question / answer - what would peoples preference be in this case
            from a view point of understanding / reading / maintaining code be?
            >
            M.
            One of the php core team covers your original question in her blog,
            check it out. In the comments she says the time difference is
            nanoseconds



            Re: Readability/easy maintenance, feel free to use one, the other or
            both, it depends on what you are doing. I've a slight preference for
            inline html but when there's a more php variables to display than html
            then I'll usually use echo.

            Comment

            • Michael Fesser

              #7
              Re: Performance of lots of ECHO's V Jumping In/Out of PHP

              ..oO(M)
              >So in that case - PHP engine scans the whole page of text, acting only on
              >code it finds between the php start / end tags?
              Yep.
              >I guess then I am wondering whether it is more efficient for the PHP engine
              >to start / stop it's processing of php code multiple times or whether it is
              >more efficient to output ECHO statements between the if conditions?
              Do you really care about some CPU cycles? It's like optimizing string
              output with single quotes instead of double quotes, but still using a
              bubble sort algo. If you get performance problems, you should run a
              profiler to see where your script wastes time. I'm sure it won't be the
              'echo' part. The bottlenecks are usually somewhere else.

              I prefer your first version, simply because the code is more readable
              and easier to maintain.

              Micha

              Comment

              • Chung Leong

                #8
                Re: Performance of lots of ECHO's V Jumping In/Out of PHP


                M wrote:
                Hi,
                Suppose you have the situation where you have say 20 blocks of text (~250
                chars each) in a MySQL db that you need to display, but each has a condition
                to check to see whether you should display it or not.
                >
                As far as I can see, you have 2 options:
                >
                1. A continuous block of php, with all the text displayed via echo commands
                >
                2. Or jumping in and out of php for each block of text i.e.
                <?php ...if condition ?>
                pure html
                <?php ...if end ?>
                >
                Which would be more performant? I understand that echo's are poor in
                performance with large blocks of text, but what is the overhead of the web
                server (Apache) making repeated calls to the php engine?
                >
                My general point is, is it "better" to echo html out (css, formatting and
                all) or continuously jump in/out of php (and only echo MySQL data) if you
                have a lot of php logic / control in your page?
                >
                Thanks,
                M
                Text outside of <?php ?is compiled to echo operations. So there's no
                basic difference between the two methods.

                Comment

                • R. Rajesh Jeba Anbiah

                  #9
                  Re: Performance of lots of ECHO's V Jumping In/Out of PHP

                  Chung Leong wrote:
                  <snip>
                  Text outside of <?php ?is compiled to echo operations. So there's no
                  basic difference between the two methods.
                  If it's true, manual is contradicting:

                  <quote src="http://in.php.net/language.basic-syntax#AEN2651" >

                  <snip>
                  } else {
                  ?>
                  <strong>This is false.</strong>
                  <?php
                  }
                  ?>

                  This works as expected, because when PHP hits the ?closing tags, it
                  simply starts outputting whatever it finds until it hits another
                  opening tag. The example given here is contrived, of course, but 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

                  • Chung Leong

                    #10
                    Re: Performance of lots of ECHO's V Jumping In/Out of PHP


                    R. Rajesh Jeba Anbiah wrote:
                    Chung Leong wrote:
                    <snip>
                    Text outside of <?php ?is compiled to echo operations. So there's no
                    basic difference between the two methods.
                    >
                    If it's true, manual is contradicting:
                    >
                    <quote src="http://in.php.net/language.basic-syntax#AEN2651" >
                    >
                    <snip>
                    } else {
                    ?>
                    <strong>This is false.</strong>
                    <?php
                    }
                    ?>
                    >
                    This works as expected, because when PHP hits the ?closing tags, it
                    simply starts outputting whatever it finds until it hits another
                    opening tag. The example given here is contrived, of course, but 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>
                    It's poorly worded in the manual. Clearly, what is expected is that the
                    conditional would determine whether the HTML appear or not. If PHP
                    simply outputs anything outside of <?php ... ?>, then both blocks would
                    always appear. Nor would the following work as "expected."

                    <?php foreach($words as $word) { ?>
                    <li><?php echo $word; ?></li>
                    <?php } ?>

                    Think about it. The way I described it is the only way it could work.
                    The PHP language engine cannot control something that isn't part of the
                    code-stream.

                    Anyway, it's in the source code. If you look in zend_compile.c, you
                    will find this:

                    switch(retval) {
                    [...]
                    case T_OPEN_TAG_WITH _ECHO:
                    retval = T_ECHO;
                    break;
                    [...]
                    }

                    retval comes from lexical analyzer (lex). As you can see, when <?php is
                    encountered, an echo op-code is added--for the text that comes before.

                    Comment

                    • R. Rajesh Jeba Anbiah

                      #11
                      Re: Performance of lots of ECHO's V Jumping In/Out of PHP

                      Chung Leong wrote:
                      <snip>
                      It's poorly worded in the manual. Clearly, what is expected is that the
                      conditional would determine whether the HTML appear or not. If PHP
                      simply outputs anything outside of <?php ... ?>, then both blocks would
                      always appear.
                      <snip>

                      Thanks for your insight.

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

                      Comment

                      Working...