简体   繁体   English

打开/关闭标签和性能?

[英]Opening/closing tags & performance?

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?这可能是一个愚蠢的问题,但作为 PHP 的新手,我想知道在 HTML 模板代码中频繁打开和关闭 PHP 标签是否存在任何与性能相关的问题,如果是,那么最佳实践可能是什么?使用 PHP 标签?

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 和 HTML 标签:

<?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:分离 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

Would be interested in hearing some views on this, even if it's just to hear that it makes no difference.有兴趣听听对此的一些看法,即使只是听说它没有区别。

Thanks.谢谢。

3 simple rules for you to get it right: 3个简单的规则,让你做对:

  • 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.即使是大的,比如说,一种方法也快 2 倍。 Oh my, 2 times!天哪,2次! I choose it and optimized my app well, it will run 50% faster now!我选择了它并很好地优化了我的应用程序,现在它的运行速度将提高 50%!

Wrong .错了 Not 50%.不是 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.因为您优化了仅占整个脚本运行时间 0,0001% 的部分。

As for the big HTML tables, it take a long time for the browser to render it.对于大的 HTML 表格,浏览器渲染它需要很长时间。 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) .可以使用一些分析器完成,比如 xdebug,甚至可以手动完成,使用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 .过早的优化是万恶之源——D.Knuth

I've redone the tests with 50,000 rows and added the multi echo in 1 tag method too我已经用 50,000 行重做了测试,并在 1 个标签方法中添加了多重回声

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持续时间 1:31.15542483 秒

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持续时间 2:30.23169804 秒

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持续时间 3:27.54640007 秒

Not much difference between the original 2 methods, but looks like it's quite a bit faster with less concatenation @poke原始的 2 种方法之间没有太大区别,但看起来它的连接速度要快一些@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.相反,您应该始终考虑性能问题以及改进它们所需的时间。

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.如果您向 php 标签发送垃圾邮件,您的代码将变得不可读。

Best solution is to use a template engine and avoid mixing code and presentation altogether.最好的解决方案是使用模板引擎并避免完全混合代码和演示。

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.时期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM