开发者

Will reducing number of includes/requires increase performance?

What is better practice for script performance tuning?

This one?

require_once("db.php");

if (!is_开发者_JAVA百科cached()) {
  require_once("somefile.php");
  require_once("somefile2.php");
  //do something
} else {
  //display something
}

Or this one?

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?

Thank you


Yes, it will increase performance and contrary to what others said, the impact may not be negligible.

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:

  • Zend Framework Performance Guide*

*The advice given there is applicable to any application or framework


Includes and requires are designed in part to allow you to organize your code and make maintaining your software easier to do. While there technically is a performance hit for using includes it is negligible and more then offset by increasing the maintainability of your software. Especially if you use an opcode cache. I wouldn't worry about this and chalk it up to premature optimization.


It can sometimes be worth it if the required files are huge, and no byte code cache is available.

The issue then, however, is not really the number of include() statements, but the amount of data that gets included. The less unused code you include in a request, the better. A good remedy to monolithic includes is splitting the code base into smaller ones, or, if your application is largely object oriented, using PHP's autoloading feature.

I've seen shared hosting packages where un-tangling monolithic includes could save up to half a second - which is a lot.

Also, included PHP code gets parsed and takes up memory, whether it's executed or not. A clean structure with lean objects is usually the optimal way.


Yes, it will increase performance, but it is very very little so it isn't worth it.


Using absolute paths for include files will increase performance significantly. Test using Apache Benchmark to see if it doesn't.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜