41

An inexperienced PHP question:

I've got a PHP script file that I need to include on different pages lots of times in lots of places.

I have the option of either breaking the included file down into several smaller files and include these on a as-needed basis... OR ... I could just keep it all together in a single PHP file.

I'm wondering if there's any performance impact of using a larger vs. smaller file for include() in this context? For example, is there any performance difference between a 200KB file and a 20KB file?

Thank you.

Tom
  • 30,090
  • 27
  • 90
  • 124

3 Answers3

48

There will be a difference, between a 200KB and a 20KB file... But you will probably not notice it : a 200KB file is not that big -- and you generally use a lot of files that are not "small", when you're building a big application.

There are two things that take time, when you're loading a .php file :

  • The PHP source code is "compiled" to "opcodes" -- that's quite equivalent to JAVA bytecode
    • This is done each time a PHP file is included, by default
    • But, using some opcode cache like APC, those opcodes can be kept in memory, and this compilation stuff not done each time anymore -- which is great : it'll mean less CPU used, as the compilation will not be done anymore (it'll be done only once in a while).
  • The opcodes are executed
    • Depending on what you script contains, this can take some time, or not :
    • If the file only contain functions or classes definitions, this will not take much time : nothing will get executed.
    • If the file contains instructions, it'll take more time ^^


As a sidnote : in a general situation, you'll gain a lot more time/cpu/resources optimizing your SQL queries, or adding some caching mecanism, than thinking about that kind of stuff.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • Thank you for the added explanation. – Tom Feb 19 '10 at 17:34
  • 2
    Even with APC, consider use of a custom __autoload function / spl_autoload_register - I was able to only include model files in a custom MVC framework where the class was invoked rather than a blanket include of all model files - this saved about 1ms per file include. So if a page render is 80 ms and you can save 20 ms through including only what you need even with APC (when there are many includes), it may be worth doing. Also only took like 10 minutes to implement. – codercake Aug 18 '11 at 21:09
16

Be careful with include_once() (and also require_once()), it is more expensive to run than include(). Every time include_once() is run, PHP does a lookup against an internal index of already included files before deciding whether to load the file or not. The more includes in the index, the slower this lookup is. Also when using include() or include_once() try to use absolute paths where possible as this is much speedier than relative paths because you are not forcing PHP to work out the absolute path for you. As ggiroux said, some form of caching like APC will reap massive rewards and render worrying about how many include calls you have irrelevant (largely) (unless you have some poorly written code).

EDIT--

Worrying about the above calls is only an issue once you to start have several thousand requires or includes in your codebase.

James Butler
  • 3,852
  • 1
  • 26
  • 38
  • On the web, every 100 ms counts. You don't need thousand of include to benefit from this hugely. – Pacerier Oct 16 '14 at 20:33
  • 1
    What do you think about this strange post: https://seeit.org/2010/06/11/php-the-include-include_once-performance-debate/ which claims that **include_once()** is actually faster then **include()**? – Adam Apr 26 '16 at 10:10
  • 1
    The gain from *_once is that you don't include files several times, which will likely take a lot longer than checking. – Mantriur Sep 21 '22 at 15:06
1

There is certainly an impact, so be sure to use include_once() instead of include(). You could maybe consider using APC which has an include cache.

ggiroux
  • 6,544
  • 1
  • 22
  • 23
  • ggiroux... thanks... what about when the file IS cached? Unless that's a really silly question. – Tom Feb 19 '10 at 17:16
  • 1
    if it's cached I would say that the impact is very minimal, as it will be stored in precompiled bytecode in shared memory. – ggiroux Feb 19 '10 at 17:21
  • 4
    include_once() instead of include() ? Please expand on that; it certainly does not add to speed to use include_once for the reasons already mentioned in this thread already. – Slavic Mar 27 '12 at 14:10