17

Possible Duplicate:
Efficiency for including files of functions (in PHP)
Using too much include() in php

If so, what would be the recommended amount of files to include at most?

Community
  • 1
  • 1
Zerbu
  • 555
  • 1
  • 5
  • 12
  • 3
    Including a million files will degrade performance. The question you should ask yourself is where the cutoff point is - loading a few 'large' files with a lot of code that won't get used, or many smaller files where the code WILL be used. The only way to tell is to run benchmarks on your particular setup. There's no rule that says the cut off is at "X lines of code in Y files". – Marc B May 27 '12 at 14:38
  • This is a how-long-is-a-piece-of-string question, since it depends on how many users you have, the load on your current servers, what your web server configuration is, whether you have a PHP accelerator (etc). My view is built your project with as many or as few as you like, and then worry about optimisation when you actually have to. (That said, my symfony projects will include probably 50-100 files during each request, so you can have quite a few and still have good performance - if your server is suitably configured). – halfer May 27 '12 at 14:41
  • @MarcB, Thousands will degrade performance, doesn't need to get anywhere near a million. – Pacerier Oct 16 '14 at 20:38

2 Answers2

24

Including a file will do the following things:

  • Read the file from disk.
  • Run the code in the file.

Both of these operations take time. Not much time, but even so it can add up if you have a lot of includes, so the basic answer to your question is 'yes, it can affect performance'.

However, the size of that performance hit is pretty tiny, and is far outweighed by the advantages of writing good quality well structured code (which includes keeping separate classes/functionality in separate files).

Truth be told, if you're worried about the performance of these sorts of things, try running your code through a profiler such as xDebug. This will show you exactly what parts of your code are taking the most amount of time. include() statements will show up in there, but are very unlikely to be anywhere near the top of the list.

Unless you're writing a site with visitor numbers like Facebook, then you're unlikely to need to worry about the performance of include(). But take a look at the profiler output from xDebug, because there are likely to be other things in your code that are going much slower than you expected, or are being called to often, or are looping too many times, etc, and fixing these will have a big impact on the performance of your code.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • You call "reading the file from disk" not much time? Then why does accelerators achieve over x10 speed? – Pacerier Oct 16 '14 at 20:39
  • 3
    @Pacerier - depends what accelerators you're referring to? If you mean things like Opcache and APC, then they do a lot more than caching the file loads; they also cache the compilation step, which is where the real performance boost comes from. If you really want to see what kind of perf boost is gained by reducing includes, look at the Phar mechanism: Phar files are PHP bundles; effectively all the files for a program combined into a single file to make distribution easier. There is a perf benefit, but not really that much; it's mainly done for ease of distribution. – Spudley Oct 16 '14 at 21:44
1

The include directive does "paste" the contents of the included file at the very same spot where it stands. So the included code adds up to the actual script. Here's a good explanation PHP include(): File size & performance - the accepted answer.

Community
  • 1
  • 1
Havelock
  • 6,913
  • 4
  • 34
  • 42