SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2000
    Location
    Bangkok,Thailand
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP and HTML performance issue

    Hiya,

    When writing ASP pages, there is a marked improvement in performance if you minimise the switch between vb script and HTML by using response.write to echo the HTML code and to skip using the <%=whatever%> shortcuts.

    What I am wondering is does the same hold for PHP? If instead of jumping in and out of PHP code in a page of HTML I coded the whole lot in PHP would there be an improvement in performance?

    Much of the HTML could be run in functions (eg open and closeing tables springs to mind) of just echo using echo, print, printf etc.

    Does anyone have any opinions on this, or any references to benchmark sites I can take a look at?

    Thanks in advance.

    Stuart
    http://www.travelfish.org

  2. #2
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There have been some benchmark's run by members here, and if my memory serves me correct the general concensus was that it was slightly faster to jump in and out.

    There are others somewhere, but you could use robo's code on this page to test it for yourself:

    http://www.sitepointforums.com/showt...threadid=27124
    Oh no! the coots are eating my nodes!

  3. #3
    code addict Abstraction's Avatar
    Join Date
    Apr 2001
    Location
    Des Moines, IA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't feel the slight performance gain is worth it though. Jumping in and out makes the code hard to read, not to mention in encourages sloppy code.

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think it makes it easy to read. but that's just me...
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  5. #5
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To me a 2000 line PHP file with like:
    echo '<html>';
    etc, all the way through looks ugly, but I suppose that is why I use phplib's template class a lot, and functions where necessary.
    Oh no! the coots are eating my nodes!

  6. #6
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP runs faster when you run as much echoing as possible OUT of the realm where PHP will have to echo it out. It compiles the PHP code, then runs it, not running as it goes.

    It makes work, especially when you're using a large number of tables, fast to code when you can just jump out of the scripting engine, dump it, then go back to the processing engine.... I just use <?=$var?> an awful lot

    So for example:

    PHP Code:
    <html>
    <head>
    <title><?=$title?></title>
    </head>
    <body background="<?=$image?>">
    <table border="<?=$border?>">
    <?php
    $i 
    0;
    while(
    $i 50){
       if((
    $i 2) == 0){
          
    $bgcolor "red";
       } else {
          
    $bgcolor "green";
      } 
      
    ?>
      <tr bgcolor="<?=$bgcolor?>"><td><?=$i?></td></tr>

       <?php
       $i
    ++
    }
    ?>
    </table>
    </body>
    </html>
    Would in theory be faster than:

    PHP Code:
    echo("<html>
    <head>
    <title>
    $title</title>
    </head>
    <body background=\"
    $image\">
    <table border=\"
    $border\">");

    $i 0;
    while(
    $i 50){
       if((
    $i 2) == 0){
          
    $bgcolor "red";
       } else {
          
    $bgcolor "green";
      } 

       echo(
    "<tr bgcolor=\"$bgcolor\"><td>$i</td></tr>");

       
    $i++
    }
    echo(
    "</table>
    </body>
    </html>"
    );
    ?> 
    I personaly like to use Adoby GoLive 5.o for my web development, and use it to create complex table layouts and other stuff. I then insert into the <noedit> portion of GoLive my needed PHP and I get excellent results. If I remeber correctly it isn't enough of a gain for developers to worry about, if you code one way, then there is no reason to change because the gain is so minute.
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2000
    Location
    Bangkok,Thailand
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    interesting

    I find what you say interesting as it is almost exactly the opposite to the case with ASP where removing the short-cuts <%=whatever%> and running the response.writes together is definetely faster. In some cases yielding as much as a 50% improvement in speed (including moving to stored procedures) and about 20% without SP's.

    It seems odd, as you'd assume that asp and php are effectivly doing and working in the same way.

    Thanks anyway
    s
    http://www.travelfish.org

  8. #8
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I could say that is because ASP su....

    but then, I know the truth hurts some people.
    Oh no! the coots are eating my nodes!

  9. #9
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually ASP is useful for enterprise level development, and it's what many large companies use, so obviously it does what they want it to.

    PHP is a VERY loosly typed language, which is cool for us because we can get the same job with several different approaches. However, ASP teaches the programmer to code tighter, and therefore produces faster code. ASP is just DIFFERENT than PHP, and I, myself, sue them for DIFFERENT uses. Like just recently Exbabylon had to integrate a customer login with a regional ISP's program. Guess what? It was on win2K and MS SQL... fun huh? not really. I could've done it in PHP, sure, but ASP was so simple, fast, efficient....

    Oh, and the one advantage PHP has is Zend.
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  10. #10
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    I like exiting out of the PHP to print certain HTML things that I need to do. Like this:
    PHP Code:
    <?
    $color 
    "red";

    if(
    $color == "red")
    {
    ?>
    <HTML>
    <HEAD>
    <TITLE>Red</TITLE>
    </HEAD>
    <BODY BGCOLOR="red">
    RED PAGE
    </BODY>
    </HTML>
    <?
    }
    else
    {
    ?>
    <HTML>
    <HEAD>
    <TITLE>White</TITLE>
    </HEAD>
    <BODY BGCOLOR="white">
    WHITE PAGE
    </BODY>
    </HTML>
    <?
    }
    ?>
    Yes this might be a little extreme but you get the point

  11. #11
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops, I think when I started that word with two letters you thought I was going to say something derogatory and bad about ASP

    Instead I was leaving it open so that you could fill it in with anything you liked, I was thinking of two examples:
    ...because ASP su*** PHP's butt // makes asp sound good in a twisted way
    and
    ...because ASP su*** // makes asp sound bad

    Believe what you will, I'm not entering or starting a debate.
    Oh no! the coots are eating my nodes!

  12. #12
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No one really seems to have answered the performance issues of jumping in and out of the coding very well, as was the primary purpose of this thread.

    Answering whether jumping in and out of PHP code has an performance effects: it does slow down performance a little bit everytime you switch between <?php ?> and HTML coding.

    The reason being that each time the PHP script begins anew, the PHP parser has to re-initialize to execute that coding -- which in turn causes more overhead and a slight increase in server processes.

    So generally it is fine, but if you have an enourmouse script in which you exit and re-enter the PHP coding tags many times, it's going to have a negative effect on your performance because the PHP parser engine has to initialize, un-load, re-initialize, un-load, and so on and so forth.

    So, unless you're dealing with a really small file, you're best bet is to keep the number of switches between your PHP coding and your HTML coding at a minimum.

    Happy coding!
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  13. #13
    SitePoint Enthusiast
    Join Date
    May 2000
    Location
    Bangkok,Thailand
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thankyou

    To timnz, Most of my *fun* work is with php/mysql, most of my *paid* work is with asp/sql7/2000 - hence the question.

    To Aes, Yes, that was what I was looking for. I was intrigued about this issue as there has been MUCH written about it with regard to ASP, but precious little from a PHP viewpoint

    Thanks

    stuart
    http://www.travelfish.org

  14. #14
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aes, your wisdom seems to be different from that of exbabylon's.

    And for me, when I ran a small "benchmark" it was faster jumping in and out of PHP than echo'ing it out, although I acknowledge that this could be drastically changed due to different environmental conditions, and a much larger file than the one I tested with.
    Oh no! the coots are eating my nodes!

  15. #15
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Aes
    The reason being that each time the PHP script begins anew, the PHP parser has to re-initialize to execute that coding -- which in turn causes more overhead and a slight increase in server processes.

    So generally it is fine, but if you have an enourmouse script in which you exit and re-enter the PHP coding tags many times, it's going to have a negative effect on your performance because the PHP parser engine has to initialize, un-load, re-initialize, un-load, and so on and so forth.
    That's not correct, the PHP engine will load only once per file when in CGI-mode; when using mod_php the engine stays in memory so there isn't even that overhead.
    ck :: bringing chris to the masses.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •