PHP include(): 文件大小&表现
一个没有经验的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
200KB 和 20KB 文件之间会有区别...但您可能不会注意到这一点:200KB 文件并没有那么大,而且您通常会使用很多不“小”的文件,当您正在构建一个大型应用程序。
当您加载
.php
文件时,有两件事需要花费时间:作为旁注:在一般情况下,您将获得更多的时间/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 :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.
请小心
include_once()
(以及require_once()
),它的运行成本比include()
更高。每次运行include_once()
时,PHP 都会在决定是否加载文件之前对已包含文件的内部索引进行查找。索引中包含的内容越多,查找速度就越慢。另外,当使用
include()
或include_once()
时,请尽可能使用绝对路径,因为这比相对路径要快得多,因为您不会强迫 PHP 计算出绝对路径为你的道路。正如 ggiroux 所说,像 APC 这样的某种形式的缓存将获得巨大的回报,并让您担心有多少包含调用是不相关的(很大程度上)(除非您有一些写得不好的代码)。
编辑 -
一旦您的代码库中开始有数千个
requires
或includes
,担心上述调用只是一个问题。Be careful with
include_once()
(and alsorequire_once()
), it is more expensive to run thaninclude()
. Every timeinclude_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()
orinclude_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
orincludes
in your codebase.肯定有影响,所以一定要使用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.