Programing

열기 / 닫기 태그 및 성능?

lottogame 2020. 9. 7. 22:27
반응형

열기 / 닫기 태그 및 성능?


이것은 어리석은 질문 일 수 있지만 비교적 PHP를 처음 접하는 사람으로서 HTML 템플릿 코드에서 PHP 태그를 자주 열고 닫는 데 성능 관련 문제가 있는지 궁금합니다. 그렇다면 가장 좋은 방법은 무엇일까요? PHP 태그로 작업하는 방법?

내 질문은 닫는 태그의 중요성 / 정확성 또는 다른 유형보다 더 읽기 쉬운 코드 유형에 관한 것이 아니라 문서가 구문 분석 / 실행되는 방법 및 성능에 미치는 영향에 대한 것입니다.

설명을 위해 다음 두 가지 극단을 고려하십시오.

PHP와 HTML 태그 혼합 :

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

PHP 및 HTML 태그 분리 :

<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

차이가 없다는 것을 듣는 것이라도 이것에 대한 몇 가지 견해를 듣고 싶을 것입니다.

감사.


제대로하기위한 3 가지 간단한 규칙 :

  • 구문 문제는 성능에 영향을 줄 수 없습니다. 데이터 조작이 수행됩니다.
  • 프로파일 링 결과로만 뒷받침되는 성능에 대해 이야기하십시오 .
  • 조기 최적화는 모든 악의 근원입니다

성능 문제는 이해하기 매우 어렵습니다. 초보자는 그것을 고려하지 않는 것이 좋습니다. 그들은 항상 사소한 것들에 감명을 받고 진짜 중요한 것들을 보지 못하기 때문입니다. 경험 부족 때문입니다.

귀하의 질문과 동일합니다. 당신이 어떤 차이를 얻을 것이라고 상상해보십시오. 큰 것조차도 한 가지 방법이 2 배 더 빠릅니다. 오 마이, 2 번! 나는 그것을 선택하고 내 앱을 잘 최적화했습니다. 이제 50 % 더 빠르게 실행됩니다!

틀렸어 . 50 %가 아닙니다. 이 속도 증가를 알아 차 리거나 측정하지도 않습니다. 전체 스크립트 런타임의 0,0001 % 만 차지하는 부분을 최적화했기 때문입니다.

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. I'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, you may 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.


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


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.


Code that is easy to translate to pseudo-code is better. This is evidenced by the examples above. Which takes longer to say?

"Start php, do this 30 times:, then stop php.  Print this.  Start php, print this, stop php. Print this.  Start php, print this, stop php.Print this.  Start php, print this, stop php. Print this.  Start php, print this, stop php.Print this.  Start php, print this, stop php. Print this.  Start php, print this, stop php.Print this.  Start php, print this, stop php..."

"Start php, do this 30 times: print this, then add this to that, then add this to that, then add this to that, then add this to that, then add this to that, then add this to that..."

"Start php, do this 30 times: print this, print this, print this, print this, print this, print this, print this..."

Personally I would do:

"Start php, define this, do this 30 times: add this to that.  Print." 

A technical explanation about how the interpreter works and why one way is faster than another is irrelevant for a newbie. It is best just to know the rules of thumb:

  1. Simpler is better.
  2. If it doesn't fit on a single page then it is doing too much (break it down).
  3. If you cannot hand-write the pseudo-code on an index card, it is too complex.

Use more tags if the overall result is simpler. Period.


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.

참고URL : https://stackoverflow.com/questions/2437144/opening-closing-tags-performance

반응형