php 文件是否完全加载了条件语句?

发布于 2024-12-11 15:37:55 字数 219 浏览 0 评论 0原文

我想知道当文件包含条件语句时是否会完全加载文件。

假设我有一个包含大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奈何桥上唱咆哮 2024-12-18 15:37:55

回答问题的第一部分很简单:是的,文件会被完整加载以便进行解析。一半或全部代码最终不被执行并不重要。

第二部分更加棘手。您正在谈论性能,但这个特定领域(尝试通过优化包含的数量和大小来提取更多性能)是相当悬而未决的成果。因此:

  1. 您是否已经耗尽了所有可能的性能来做更简单的事情,例如设置资产缓存、操作码缓存等?在达到这一点之前你应该先了解一下。
  2. 无论如何也不可能为你回答这个问题;人们需要只有您才能访问的真实性能数据。

根据经验,我相信您所担心的事情只会以微不足道的方式影响您的底线。停止这样做,只写好的、可维护的代码。根据需要分割源代码并使用自动加载器是实现此目的的好方法。

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:

  1. Have you drained every last drop of performance possible doing easier stuff, like setting up asset caching, opcode caching, etc? You should have before getting to this point.
  2. It is impossible to answer the question for you anyway; one would need real performance data that only you have access to.

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.

帝王念 2024-12-18 15:37:55

首先,过早的优化:不要这样做。其次,如果 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.

百善笑为先 2024-12-18 15:37:55

为什么还要想到这个?!安装 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文