Opening/closing tags & performance?

12,286

Solution 1

3 simple rules for you to get it right:

  • No syntax issue can affect performance. Data manipulation does.
  • Speak of performance only backed with results of profiling.
  • Premature optimization is the root of all evil

Performance issues are quite hard to understand. It is advised for the newbies not to take it into account. Because they are always impressed with trifle things and fail to see a real important things. Just because lack of experience.

Same for your question. Imagine you'll ever get some difference. Even big one, say, one method is 2 times faster. Oh my, 2 times! I choose it and optimized my app well, it will run 50% faster now!

Wrong. Not 50%. You'd never notice or even measure this speed increase. Because you optimized a part that take only 0,0001% of whole script runtime.

As for the big HTML tables, it take a long time for the browser to render it. Much more than you took to generate.

Profiling is a key word in the performance world. One can trash any performance related question with no doubts if there is no word "profiling" in it. At the same time profiling is not a rocket science. It's just measuring of runtime of different parts of your script. Can be done with some profiler, like xdebug, or even manually, using microtime(1). And only after detecting the slowest part, may you start with tests.

Learn to profile before asking performance questions. And learn not to ask performance questions if there is no real reasons for it.

Premature optimization is the root of all evil - D.Knuth.

Solution 2

I've redone the tests with 50,000 rows and added the multi echo in 1 tag method too

for ($j=0;$j<30;$j++) {
    foreach ($results as $key=>$val){
    ?>
       <tr>
           <td><?php echo $results[$key][0]?></td>
           <td><?php echo $results[$key][1]?></td>
           <td><?php echo $results[$key][2]?></td>
           <td><?php echo $results[$key][3]?></td>
           <td><?php echo $results[$key][4]?></td>
           <td><?php echo $results[$key][5]?></td>
           <td><?php echo $results[$key][6]?></td>
           <td><?php echo $results[$key][7]?></td>
           <td><?php echo $results[$key][8]?></td>
           <td><?php echo $results[$key][9]?></td>
           <td><?php echo $results[$key][10]?></td>
           <td><?php echo $results[$key][11]?></td>
           <td><?php echo $results[$key][12]?></td>
           <td><?php echo $results[$key][13]?></td>
           <td><?php echo $results[$key][14]?></td>              
       </tr>
    <?php 
    }
}

duration1: 31.15542483 Seconds

for ($k=0;$k<30;$k++) {
    foreach ($results as $key1=>$val1){
        echo
           '<tr>
               <td>'.$results[$key1][0].'</td>
               <td>'.$results[$key1][1].'</td>
               <td>'.$results[$key1][2].'</td>
               <td>'.$results[$key1][3].'</td>
               <td>'.$results[$key1][4].'</td>
               <td>'.$results[$key1][5].'</td>
               <td>'.$results[$key1][6].'</td>
               <td>'.$results[$key1][7].'</td>
               <td>'.$results[$key1][8].'</td>
               <td>'.$results[$key1][9].'</td>
               <td>'.$results[$key1][10].'</td>
               <td>'.$results[$key1][11].'</td>
               <td>'.$results[$key1][12].'</td>
               <td>'.$results[$key1][13].'</td>
               <td>'.$results[$key1][14].'</td>              
           </tr>';
    }
}

duration2: 30.23169804 Seconds

for ($l=0;$l<30;$l++) {
    foreach ($results as $key2=>$val2){     
           echo'<tr>';
               echo'<td>'.$results[$key2][0].'</td>';
               echo'<td>'.$results[$key2][1].'</td>';
               echo'<td>'.$results[$key2][2].'</td>';
               echo'<td>'.$results[$key2][3].'</td>';
               echo'<td>'.$results[$key2][4].'</td>';
               echo'<td>'.$results[$key2][5].'</td>';
               echo'<td>'.$results[$key2][6].'</td>';
               echo'<td>'.$results[$key2][7].'</td>';
               echo'<td>'.$results[$key2][8].'</td>';
               echo'<td>'.$results[$key2][9].'</td>';
               echo'<td>'.$results[$key2][10].'</td>';
               echo'<td>'.$results[$key2][11].'</td>';
               echo'<td>'.$results[$key2][12].'</td>';
               echo'<td>'.$results[$key2][13].'</td>';
               echo'<td>'.$results[$key2][14].'</td>';              
           echo'</tr>';
    }
}

duration3: 27.54640007 Seconds

Not much difference between the original 2 methods, but looks like it's quite a bit faster with less concatenation @poke

Since I doubt I'll need this much data in 1 go, I guess I'll continue to use many tags, code indentation looks neater and 'view source' layout more accurate

Solution 3

You can easily ignore the performance difference between those two. With today's modern computing resources, the difference really does not matter. This kind of print-to-screen stuff are truly not to worry about. There are tons of other stuff you should be considering before. Apart from that, there is always a debate between the best performance and the maintainability of your code. You cannot always try to achieve the best performance. Instead, you should always consider performance concerns along with the amount of time you need to spend on improving them.

Solution 4

The real problem with this is memory use. String concatenation and mass echo-ing can increase memory use exponentially.

If you spam the php tag your code becomes unreadable.

Best solution is to use a template engine and avoid mixing code and presentation altogether.

Share:
12,286
Tom
Author by

Tom

I'm just here to look at the pictures.

Updated on June 08, 2022

Comments

  • Tom
    Tom almost 2 years

    This may be a silly question, but as someone relatively new to PHP, I'm wondering if there are any performance-related issues to frequently opening and closing PHP tags in HTML template code, and if so, what might be best practices in terms of working with PHP tags?

    My question is not about the importance/correctness of closing tags, or about which type of code is more readable than another, but rather about how the document gets parsed/executed and what impact it might have on performance.

    To illustrate, consider the following two extremes:

    Mixing PHP and HTML tags:

    <?php echo
       '<tr>
           <td>'.$variable1.'</td>
           <td>'.$variable2.'</td>
           <td>'.$variable3.'</td>
           <td>'.$variable4.'</td>
           <td>'.$variable5.'</td>
       </tr>'
    ?>
    // PHP tag opened once
    

    Separating PHP and HTML tags:

    <tr>
       <td><?php echo $variable1 ?></td>
       <td><?php echo $variable2 ?></td>
       <td><?php echo $variable3 ?></td>
       <td><?php echo $variable4 ?></td>
       <td><?php echo $variable5 ?></td>
    </tr>
    // PHP tag opened five times
    

    Would be interested in hearing some views on this, even if it's just to hear that it makes no difference.

    Thanks.

  • poke
    poke about 14 years
    A third test case would be to use multiple echo statements with one php tag, as you wouldn't need to use string concatenation then.
  • Amien
    Amien about 14 years
    I've redone the tests with 50,000 rows and added the multi echo in 1 tag method too
  • NikiC
    NikiC over 13 years
    Why is it so bad if I use the word "benchmark" instead of "profile"? Is there any difference in the meaning? Would appreciate knowing the differences :)
  • Luc M
    Luc M over 13 years
    +1 Mesuring is important when we want to optimize. We often realize that this kind of optimisation is useless.
  • Mike C
    Mike C about 13 years
    From the looks of it, the first two examples output far more whitespace than the last example. That could be the reason for the higher runtime.
  • Clement Herreman
    Clement Herreman almost 13 years
    +1 for quoting Donald Knuth and +200 for very insightful answer.
  • Chuck Le Butt
    Chuck Le Butt almost 13 years
    @nikic, When did Col say that it was bad to use the word "benchmark"?? Sounds like you're putting words into his mouth, or referencing something in the wrong place.
  • Chuck Le Butt
    Chuck Le Butt almost 13 years
    I've heard programmers get quite angry about IF statements vs CASE statements. Are you saying there's no real difference in terms of performance?
  • hakre
    hakre over 11 years
    Also echo accepts multiple expressions to be output. No variant with that feature has been considered in the metrics.
  • bharal
    bharal over 11 years
    why is this so highly rated? It doesn't answer the question in any shape or form. -1 from me.
  • Mark Amery
    Mark Amery about 10 years
    @NikiC The usual usage I've witnessed: benchmarking means somehow measuring or ranking the overall performance of piece of code in order to compare it to alternative solutions (like what Amien has done in his answer to this question, below), whereas profiling means finding out what parts of your code are responsible for whatever user-visible performance problem you're trying to solve. The difference is that profiling is about determining the cause of your performance problem, while benchmarking is about testing solutions to it.