views:

96

answers:

5

Hello,

What is better practice for script performance tuning?

This one?

require_once("db.php");

if (!is_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

+1  A: 

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

Codler
+2  A: 

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.

John Conde
+2  A: 

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.

Pekka
I have eAccelerator enabled on server
Kirzilla
@Kirzilla then I would do some profiling to find out how much can be gained.
Pekka
+2  A: 

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:

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

Gordon
Yeah, I'm already using autoloading in my applications, but exactly this application can't use autoloading because it have too much third party classes using rather strange class naming. :(
Kirzilla
@Kirzilla you could consider setting up a custom fallback autoloader that has a list of classes to files `("ThirdPartyPieChartClass" => "/tools/piecharts/index.php")` etc., and loads the desired file, instead of including them all at once.
Pekka
@Gordon Sorry, my English is not very good. As far as I understand, you mean I have to make a list of exceptions and then just do something like `if (isset($exceptions[$classname])) { require_once($exceptions[$classname]) } `? And of course before including/requiring check if classname already exists.
Kirzilla
@Kirzilla yup (Sorry @Gordon for hijacking your answer :) That code would belong in your autoloader somewhere. I don't know what your current autoloader looks like. In Zend Framework, this would be a "fallback autoloader" that gets triggered if no class file could be found in the regular name spaces.
Pekka
A: 

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

stillstanding
Yup, that's why I have only one path in include_path
Kirzilla