php 文件是否完全加载了条件语句?
我想知道当文件包含条件语句时是否会完全加载文件。
假设我有一个包含大约 5000 行代码的大文件(仅作为示例),但有一半的代码不会执行,因为它位于条件语句中。加载文件还需要同样的时间吗?
我认为不是,那么我是否也可以假设将代码拆分为多个文件并让自动加载器处理所需的文件通常会更快?或者新的文件请求会再次减慢整个过程吗?在这种情况下:当文件加载速度快于带有条件语句的文件执行速度时,边界在哪里?
I'm wondering whether a file gets loaded entirely when it contains conditional statements.
Assume I'm having a big file with about 5000 lines of code (just as an example), but half of the code wouldn't get executed because it's in a conditional statement. Would it still take the same time to load the file?
I assume not, so can I also assume that it's generally faster to split code in multiple files and let an autoloader handle the needed files ? Or would the new file request slow the whole thing down again ? In this case: Where's the border when a file load is faster than the execution of a file with conditional statements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答问题的第一部分很简单:是的,文件会被完整加载以便进行解析。一半或全部代码最终不被执行并不重要。
第二部分更加棘手。您正在谈论性能,但这个特定领域(尝试通过优化包含的数量和大小来提取更多性能)是相当悬而未决的成果。因此:
根据经验,我相信您所担心的事情只会以微不足道的方式影响您的底线。停止这样做,只写好的、可维护的代码。根据需要分割源代码并使用自动加载器是实现此目的的好方法。
Answering the first part of the question is simple: yes, files are loaded in their entirety in order to be parsed. It doesn't matter if half or all of the code will end up being not executed.
The second part is more tricky. You are talking about performance, but this specific area (trying to extract more perf by optimizing the number and size of includes) is pretty high-hanging fruit. Therefore:
Based on experience, I believe you are worrying about something that only affects your bottom line in a negligible manner. Stop doing that and just write good, maintainable code. Splitting your source code as much as you like and using an autoloader is a good approach to doing that.
首先,过早的优化:不要这样做。其次,如果
else
语句中有 2500 行,那么您真的应该拿起一本关于编写干净代码的书。并回答您的问题:必须读取整个文件才能对其进行解析,这是执行之前必需的。
First of all, premature optimization: don't do it. Second, if you have 2500 lines in an
else
statement, you should really pick up a book on writing clean code.And to answer your question: the whole file has to be read in order for it to get parsed, which is required before execution.
为什么还要想到这个?!安装 APC 扩展,PHP 会将编译后的代码保留在内存中。有一种方法可以配置它,这样它就可以缓存静态代码,甚至无需接触光盘!但是,当您进行更改时,您必须手动清除缓存。
Why even think of this?! Install the APC extension and PHP will keep compiled code in memory. There is a way to configure it so it will cache static code without even touching the disc! You have to manually clear the cache though when you make changes.