减少包含/需求的数量会提高性能吗? [英] Will reducing number of includes/requires increase performance?
问题描述
什么是更好的脚本性能调整方法?
What is better practice for script performance tuning?
这个吗?
require_once("db.php");
if (!is_cached()) {
require_once("somefile.php");
require_once("somefile2.php");
//do something
} else {
//display something
}
还是这个?
require_once("db.php");
require_once("somefile.php");
require_once("somefile2.php");
if (!is_cached()) {
//do something
} else {
//display something
}
是否值得在流量控制结构中包含/要求包含或不包含?
Is it worth placing includes/requires into flow control structures or no?
谢谢
推荐答案
是的,它将提高性能,并且与其他人所说的相反,影响可能微不足道.
Yes, it will increase performance and contrary to what others said, the impact may not be negligible.
当您包含/需要文件时,PHP将检查所有已定义的include_paths,直到找到该文件或没有其他要检查的内容为止.根据包含路径的数量和要包含的文件的数量,这将对您的应用程序产生严重影响,尤其是在预先包含每个文件时.懒惰包括(例如您的第一个示例),或者甚至更好-建议使用自动装带器.
When you include/require files, PHP will check all the defined include_paths until it finds the file or there is nothing more to check. Depending on the amount of include pathes and the amount of files to include, this will have a serious impact on your application, especially when including each and every file up front. Lazy including (like in your first example) or - even better - using the Autoloader is advisable.
有关此内容的详细阅读内容,
Related reading that addresses this in more details:
*
此处给出的建议适用于任何应用程序或框架
*
The advice given there is applicable to any application or framework
这篇关于减少包含/需求的数量会提高性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!