打开/关闭标签和表现?

2022-01-18 00:00:00 performance tags templates php html

这可能是一个愚蠢的问题,但作为一个对 PHP 比较陌生的人,我想知道在 HTML 模板代码中频繁打开和关闭 PHP 标记是否存在任何与性能相关的问题,如果是这样,什么可能是最好的使用 PHP 标签的实践?

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:

混合 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

有兴趣听听对此的一些看法,即使只是听到它没有任何区别.

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

谢谢.

推荐答案

三个简单的规则让你做对:

3 simple rules for you to get it right:

  • 没有语法问题会影响性能.数据操作确实如此.
  • 仅以分析结果为依据谈性能.
  • 过早的优化是万恶之源

性能问题很难理解.建议新手不要考虑.因为他们总是对琐碎的事情印象深刻,而看不到真正重要的事情.只是因为缺乏经验.

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.

你的问题也一样.想象一下,你会有所不同.即使是大的,比如说,一种方法也快 2 倍.哦,我的,2次!我选择了它并很好地优化了我的应用程序,它现在运行速度将提高 50%!

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!

错误.不是50%.您永远不会注意到甚至测量到这种速度增加.因为您优化了只占用整个脚本运行时间 0,0001% 的部分.

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.

对于大的 HTML 表格,浏览器需要很长时间才能呈现出来.比您生成的要多得多.

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

剖析是表演界的一个关键词.如果没有分析"一词,则可以毫无疑问地丢弃任何与性能相关的问题.在里面.同时,剖析不是一门火箭科学.它只是测量脚本不同部分的运行时间.可以使用一些分析器来完成,比如 xdebug,甚至可以手动使用 microtime(1).并且只有在检测到最慢的部分之后,才能开始测试.

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.

过早的优化是万恶之源 - D.Knuth.

相关文章