David
David

Reputation: 16716

PHP Performance on including multiple files

My current workflow I include function and class files as and when I need to. However, this can get quite messy with you have quite a lot of them in which some depend on others.

So I'm considering using a head file which includes on the files in the includes directory. But my question is, are there any PHP performance issues for doing this over including as an when i need. Often times I have to use include_once, so doing 1 big include would get rid of the need for this.

Upvotes: 2

Views: 2420

Answers (6)

Mat
Mat

Reputation: 6715

There is a performance effect but it is not a very significant one. Do whatever makes it quicker and easier for you to write the code. If down the line you find that you really need that 1ms back, and you have already trimmed all of the other fat elsewhere, then go for it. Otherwise you are throwing away development time on trying to be "perfect" when it never actually makes a practical difference.

Upvotes: 1

RJD22
RJD22

Reputation: 10340

Well including files does have a hit on the performance of your app because it needs to read your app from the disk but if you stay below about 100 files this is trivial.

Btw if you don't like having to include your class files every time check out the magic method autoload:

function __autoload($class_name) {
    include $class_name . '.php';
}

http://php.net/manual/en/language.oop5.autoload.php

Upvotes: 0

Paul DelRe
Paul DelRe

Reputation: 4039

I would recommend you look at autoloading: manual. I would also recommend using spl_autoload_register over one __autoload() function as it allows for greater control with separating out modules or namespaces.

Upvotes: 0

jwueller
jwueller

Reputation: 30996

The best approach would probably be autoloading. You do not need to (manually) include any class at all, then. Take a look at this. I recommend using the spl_autoload_register()-function. That would resolve dependencies on the fly. The performance of includes is really irrelevant in most cases. The slow things usually happen in other places. Using autoloading has the added benefit of lazy loading. You do not load source files that are not in use. This might even speed up your application.

Upvotes: 2

cusimar9
cusimar9

Reputation: 5259

PHP code is interpreted on the fly. If a given piece of code is not used, it will not be 'compiled' and so will not incur a performance hit.

However, all code on a given page (and its includes) goes through a syntax check so that may slow things down a little.

Certainly I would consider the includes that you have and whether or not you really need them.

Upvotes: 1

powtac
powtac

Reputation: 41040

Normally performance (speed) in PHP is not affected by the amount of codelines or files but of:

  • Access to db
  • Access to file system!!!
  • Access to third party APIs (SOAP...)
  • Coding style

Upvotes: 1

Related Questions