PHP include(): 文件大小&表现

发布于 2024-08-22 09:05:36 字数 234 浏览 5 评论 0原文

一个没有经验的 PHP 问题:

我有一个 PHP 脚本文件,我需要在不同页面的很多地方多次包含该文件。

我可以选择将包含的文件分解为几个较小的文件,并根据需要包含这些文件...或者...我可以将它们全部保存在一个 PHP 文件中。

我想知道在这种情况下使用较大或较小的文件进行 include() 是否会对性能产生影响?例如,200KB 文件和 20KB 文件之间是否存在性能差异?

谢谢。

An inexperienced PHP question:

I've got a PHP script file that I need to include on different pages lots of times in lots of places.

I have the option of either breaking the included file down into several smaller files and include these on a as-needed basis... OR ... I could just keep it all together in a single PHP file.

I'm wondering if there's any performance impact of using a larger vs. smaller file for include() in this context? For example, is there any performance difference between a 200KB file and a 20KB file?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

以为你会在 2024-08-29 09:05:36

200KB 和 20KB 文件之间会有区别...但您可能不会注意到这一点:200KB 文件并没有那么大,而且您通常会使用很多不“小”的文件,当您正在构建一个大型应用程序。

当您加载 .php 文件时,有两件事需要花费时间:

  • PHP 源代码被“编译”为“操作码”——这与 JAVA 字节码非常等效
    • 默认情况下,每次包含 PHP 文件时都会执行此操作
    • 但是,使用一些操作码缓存,例如 APC,这些操作码可以保存在内存中,并且这个编译工作不再每次都完成——这很好:这意味着更少的 CPU 使用,因为编译将不再完成(它只会偶尔完成一次)

  • 操作码被执行
    • 根据您的脚本包含的内容,这可能需要一些时间,也可能不需要:
    • 如果文件仅包含函数或类定义,则不会花费太多时间:不会执行任何操作。
    • 如果文件包含说明,则需要更多时间 ^^

作为旁注:在一般情况下,您将获得更多的时间/CPU/资源来优化 SQL 查询,或添加一些缓存机制,而不是考虑此类事情。

There will be a difference, between a 200KB and a 20KB file... But you will probably not notice it : a 200KB file is not that big -- and you generally use a lot of files that are not "small", when you're building a big application.

There are two things that take time, when you're loading a .php file :

  • The PHP source code is "compiled" to "opcodes" -- that's quite equivalent to JAVA bytecode
    • This is done each time a PHP file is included, by default
    • But, using some opcode cache like APC, those opcodes can be kept in memory, and this compilation stuff not done each time anymore -- which is great : it'll mean less CPU used, as the compilation will not be done anymore (it'll be done only once in a while).
  • The opcodes are executed
    • Depending on what you script contains, this can take some time, or not :
    • If the file only contain functions or classes definitions, this will not take much time : nothing will get executed.
    • If the file contains instructions, it'll take more time ^^

As a sidnote : in a general situation, you'll gain a lot more time/cpu/resources optimizing your SQL queries, or adding some caching mecanism, than thinking about that kind of stuff.

安静被遗忘 2024-08-29 09:05:36

请小心 include_once()(以及 require_once()),它的运行成本比 include() 更高。每次运行 include_once() 时,PHP 都会在决定是否加载文件之前对已包含文件的内部索引进行查找。索引中包含的内容越多,查找速度就越慢。
另外,当使用 include()include_once() 时,请尽可能使用绝对路径,因为这比相对路径要快得多,因为您不会强迫 PHP 计算出绝对路径为你的道路。
正如 ggiroux 所说,像 APC 这样的某种形式的缓存将获得巨大的回报,并让您担心有多少包含调用是不相关的(很大程度上)(除非您有一些写得不好的代码)。

编辑 -

一旦您的代码库中开始有数千个 requiresincludes ,担心上述调用只是一个问题。

Be careful with include_once() (and also require_once()), it is more expensive to run than include(). Every time include_once() is run, PHP does a lookup against an internal index of already included files before deciding whether to load the file or not. The more includes in the index, the slower this lookup is.
Also when using include() or include_once() try to use absolute paths where possible as this is much speedier than relative paths because you are not forcing PHP to work out the absolute path for you.
As ggiroux said, some form of caching like APC will reap massive rewards and render worrying about how many include calls you have irrelevant (largely) (unless you have some poorly written code).

EDIT--

Worrying about the above calls is only an issue once you to start have several thousand requires or includes in your codebase.

盗梦空间 2024-08-29 09:05:36

肯定有影响,所以一定要使用include_once()而不是include()。您也许可以考虑使用具有包含缓存的 APC。

There is certainly an impact, so be sure to use include_once() instead of include(). You could maybe consider using APC which has an include cache.

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