Speed Comparison for echo

Discussion in 'PHP' started by ketan9, Feb 25, 2007.

  1. #1
    Has any one done comparison between the following statements:
    <?php 
     echo 'This is all the html content printout with php variables, 
       like i =  ' . $i ;
    ?>
    
    PHP:
    versus

    
    <?php
    echo <<<EOQ
        This is all the html content printout with php variables, 
       like i = $i
    EOQ;
    ?>
    
    PHP:
    I find using "echo <<<EOQ " very convenient but was wondering if it has some speed bumps compared to simple echo? I searched on net but couldn't come up quickly with reasonable answer.. So you tell me!!
     
    ketan9, Feb 25, 2007 IP
  2. rays

    rays Active Member

    Messages:
    563
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    echo "string" is much faster ... as compile time for this statement is less than the <<<EOQ EOQ; ...as in case of EOQ it needs to do lot of tracking for escape sequences...
    Let me know members if i am correct .. as both methods are useful .... but i prefer to use echo "";
     
    rays, Feb 26, 2007 IP
  3. melado

    melado Member

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    38
    #3
    I think it's better to use ' ' instead of ", because it doesn't have to parse the content, it simply shows it.
     
    melado, Feb 26, 2007 IP
  4. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #4
    It really makes little difference, all three methods are just as good as each other, any speed gains over each other would be minimal.
     
    SilkySmooth, Feb 26, 2007 IP
  5. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #5
    echo with single quotes is faster than the same using the heredoc syntax.

    Source
     
    wmtips, Feb 27, 2007 IP
  6. Selkirk

    Selkirk Peon

    Messages:
    93
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Heredoc was slow in older versions of PHP. I think its been fixed in recent versions. There is no difference at all between single and double quotes. There IS a difference between:
    echo "one" . " " . "two";
    PHP:
    and
    echo "one", " ", "two"; 
    PHP:
    Notice the use of commas instead of concatenation.
    If you care about performance, use an opcode cache. This stuff isn't worth worrying about.
     
    Selkirk, Feb 27, 2007 IP
  7. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Of course there is a difference. Do you not think that it's faster when the parser doesn't have to go through a string trying to find variables it has to parse? If you have single quotes it just gets displayed as is. :rolleyes:
     
    Icheb, Feb 27, 2007 IP
  8. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #8
    A question along these lines came up at Vancouver PHP Conference earlier this month and core developers there said this is one of the last things to consider when speeding up apps.

    The first thing to do is run your PHP code throughthe Callgrind module in Valgrind. This will help you figure out where your code is spending most of its time, pointing you in the correct direction for optimizing code.
     
    clancey, Feb 27, 2007 IP
  9. Selkirk

    Selkirk Peon

    Messages:
    93
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Recognizing strings is the job of the scanner, not the parser. The factors that influence its performance are not intuitive. I've traced the scanning code looking into this particular issue. I'm guessing you haven't. :rolleyes:

    There is nothing to see here. Move along.

    If you really want to have an impact on performance, use this tool.
     
    Selkirk, Feb 28, 2007 IP