Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does including too many files in PHP reduce performance? [duplicate]

Tags:

include

php

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?

like image 610
Zerbu Avatar asked May 27 '12 14:05

Zerbu


People also ask

How many PHP files do I need?

Each PHP file uses around 10 includes. So for every page that is displayed, the server needs to fetch 10 files, in addition to the rest of its functions (MySQL, etc).

Do PHP includes slow down?

Yes it will slowdown if you are including the script from other servers.

Can we include one PHP file multiple times?

If the file is included twice, PHP will raise a fatal error because the functions were already declared. It is recommended to use include_once instead of checking if the file was already included and conditionally return inside the included file.

Do PHP comments affect performance?

But as for execution speed, it has no influence, because they are just ignored by the interpreter. So basically, it does not matter if you add comments.


1 Answers

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.

like image 190
Spudley Avatar answered Sep 20 '22 13:09

Spudley