18

Possible Duplicate:
Why is require_once so bad to use?

I've read somewhere that the include_once and require_once statements in PHP were slower than their non-once counterparts. Is this a significant slowdown? Has there been any testing or studies of this, and has it changed in recent versions of PHP?

Community
  • 1
  • 1
Ricket
  • 33,368
  • 30
  • 112
  • 143

2 Answers2

22

The speed increase is minimal and comes as a reference check is conducted to prevent code duplication. The 'once' appendage is a preventative measure against the same code being executed/included twice..this performing this check comes at a minor speed cost.

If there is ever an instance where you are using _once look into why it is the case, is you code really built in the most efficient way? It is often better to remove the need to rely on _once and produce better code (easier said than done!).

See:

http://forums.digitalpoint.com/showthread.php?t=1693837

http://www.phpbb.com/community/viewtopic.php?f=71&t=565933

http://www.sitepoint.com/forums/showthread.php?t=269085

http://www.quora.com/What-is-the-difference-between-functions-include-and-include_once-in-PHP

SW4
  • 69,876
  • 20
  • 132
  • 137
4

The include_once and require_once functions are slower than include and require, simply because they keep track of the files that have already been included, to avoid including them more than once.

But that shouldn't matter AT ALL, since there are probably many ways to optimize your application, way more efficient than this one.

Nicolas
  • 2,158
  • 1
  • 17
  • 25
  • 1
    Just to clarify -- the `include_once` and `require_once` functions are only slower when the script is run *exactly* once. If a particular script `foo.php` is included via `include`, then it is run each and every time. If it is included via `include_once`, then there is a minimal speed cost to check if it has been run, but on every run but the first, the script is not executed. – jvriesem Aug 26 '14 at 04:31