SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Do excessive PHP comments in empty PHP tags slow things down?

    I'm talking about when PHP comments in empty PHP tags are mixed with HTML. For example:
    PHP Code:
    <?php /* begin #wrapper */ ?>
    <div id="wrapper">

      <?php /* begin #header */ ?>
      <div id="header"></div>
      <?php /* end #header */ ?>

      <?php /* begin #navigation */ ?>
      <div id="navigation"></div>
      <?php /* end #navigation */ ?>

      <?php /* begin #sidebar */ ?>
      <div id="sidebar"></div>
      <?php /* end #sidebar */ ?>

    </div>
    <?php /* end #wrapper */ ?>
    Notice I'm using PHP comments in the template so as not to litter the page with regular HTML comments (<!-- like so -->)

    I could have nearly 30-40 such "empty" php comment tags, so I'm wondering if the parser is wasting time and resource delving into these empty php tags and could it be causing a performance hit?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    The only part of the process that would even see those comments is the PHP compiler that the code passes into when it is first called. It would take a negligible amount of time to read in your page regardless of the comments and the compiled code without the comments is what then gets run and so the difference would probably be too small to notice.

    Slower than where a language gets compiled once rather than every time the page is called but a lot lot faster than if it were an interpreted scripting language where comments inside loops would be processed every time around.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why on earth do you need comments like:
    Code html:
    <?php /* begin #wrapper */ ?> 
    <div id="wrapper"> 
     
      <?php /* begin #header */ ?> 
      <div id="header"></div> 
      <?php /* end #header */ ?> 
     
      <?php /* begin #navigation */ ?> 
      <div id="navigation"></div> 
      <?php /* end #navigation */ ?> 
     
      <?php /* begin #sidebar */ ?> 
      <div id="sidebar"></div> 
      <?php /* end #sidebar */ ?> 
     
    </div> 
    <?php /* end #wrapper */ ?>

    Ok, I understand a plausible use for the end ones, but the beginning ones are simply pointless.

    The code itself tells you what's going on - HTML is very self-documenting by nature, and if you indent properly (which, it seems, you do), you should have no problems knowing what an ending tag is actually ending.

    Commenting is often misused. It ISN'T for pointing out the obvious, it's to tell future developers (including yourself) what a certain thing does, or why it's there - only when it isn't already obvious.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    Serial Site Creator ToddW's Avatar
    Join Date
    Feb 2004
    Posts
    792
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I`m hoping that was an example as it seems easy to read w/out the comments

  5. #5
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Ok, I understand a plausible use for the end ones, but the beginning ones are simply pointless.
    As ToddW mentioned above, the code I posted was only for example purposes to give folks an understanding of the question.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    If you are all concerned about it, then use a byte code cache like APC or any others. But do not change the code just for a couple of microseconds. A byte code cache will save a lot more then stripping whitespace and comments.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Short answer, yes.

    Long answer, it's going to take a lot of them to make a noticeable difference. Technically a page with 500 PHP comment blocks like that should be faster to execute than a page with 500 echo statements because the parser simply throws away the comment token when it comes across it whereas it has to do further processing with the echo statements.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by joebert View Post
    Short answer, yes.

    Long answer, it's going to take a lot of them to make a noticeable difference. Technically a page with 500 PHP comment blocks like that should be faster to execute than a page with 500 echo statements because the parser simply throws away the comment token when it comes across it whereas it has to do further processing with the echo statements.
    Short answer if you do what logic_earth suggested is - NO. Since if you use a byte cache it only has to compile the code once instead of every page access you'd need millions of comments to add a few seconds to the initial compile time.

    Short answer even if you don't do that is stll NO - unless you have tens of thousands of such comments in the page as the compiler still only has to discard each one once when it compiles the page.

    If it is too short for a real person to notice then it isn't worth worrying about.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems like a waste of time to create such comments.

    There are many more places than necessary where a bug could appear in the code that could cause the page to malfunction.

    When it is obvious, the code should be self-documenting.

    Just do this:

    Code:
    echo '<div id="wrapper">
         <div id="header"></div>
         <div id="navigation"></div>
          <div id="sidebar"></div>
    </div>';

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Short answer even if you don't do that is stll NO - unless you have tens of thousands of such comments in the page as the compiler still only has to discard each one once when it compiles the page.
    Depends on how literal you take the original question. Technically the answer is yes because it does slow things down; though the difference is microscopic, there is still a difference. In a practical sense, no it doesn't slow things down.

    If you had a stopwatch and timed the time it takes from pressing F5 to the page to finish loading, three times with comments and three times without, you shouldn't notice a time difference because the time difference is much smaller than the lowest time fraction on a stopwatch - plus human reaction times are laughable compared to a computer's
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  11. #11
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by kronso View Post
    It seems like a waste of time to create such comments.
    The OP already said that was only an example. Presumably the real comments they are talking about are far more useful.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •