Zend Certified Engineer

phpguru.org

Free PHP, Javascript and C# code

require() vs require_once()


4th March 2008, 929 views

Just found out that require_once() is actually slower than require(). Since I use it for all of my projects, I'd have to say "Oops..."

> You are much better off using a
> straight include or require call,
> because the *_once() calls are very slow
> under an opcode cache. Sometimes there
> is no way around using these calls,
> but recognize that each one costs
> you an extra open() syscall and
> hash look up.


Note Due to IE now showing Rasmus's page incorrectly, I didn't (could't) see the date on it. It's two years old. But apparently still valid if you're using a pre 5.2.x version of PHP.

Top 10 referrering pages

  1. http://www.google.com/search?hl=en&q=php+performa... (5 referrals)
  2. http://www.google.com/search?q=php+require+vs+req... (5 referrals)
  3. http://www.google.com/search?hl=en&q=require+vs+r... (3 referrals)
  4. http://www.google.com/search?hl=en&q=require_once... (3 referrals)
  5. http://www.google.com/search?q=require_once&rls=c... (3 referrals)
  6. http://www.google.co.uk/search?hl=en&q=require+re... (2 referrals)
  7. http://www.google.com/search?client=firefox-a&rls... (2 referrals)
  8. http://www.google.ca/search?q=require_once&ie=utf... (2 referrals)
  9. http://www.google.com/search?hl=en&q=require_once... (2 referrals)
  10. http://www.google.com/search?q=require+vs+require... (2 referrals)
- +
Rate this article

Link to me

If you use any of the code on this site (and if you don't I guess) or it makes your life easier, I'd appreciate a link - http://www.phpguru.org. Thanks!

RSS Feed for Comments

Comments

Author: Rafael S. de Sá
Posted: 4th March 2008 14:23
Nice one, I always use require_once in my scripts, I´ll re-write all my code.
Quote
Author: Richard Heyes
Posted: 4th March 2008 14:43
Rafael S. de Sá:
> Nice one, I always use require_once in my
> scripts, I´ll re-write all my code.

I wouldn't recommend it. I would suggest to change them as required.
Quote
Author: Jeremy Privett
Posted: 4th March 2008 15:05
Yeah ... I've always used require_once in my projects, up until now. The speed difference seems pretty negligible, to me.

This is probably one of those optimization points that you don't really need to care about unless you are having to get literally milliseconds of extra performance out of your code.
Quote
Author: Richard Heyes
Posted: 4th March 2008 15:20
Jeremy Privett:
> Yeah ... I've always used require_once in my
> projects, up until now. The speed difference
> seems pretty negligible, to me.
>
> This is probably one of those optimization
> points that you don't really need to care about
> unless you are having to get literally
> milliseconds of extra performance out of your
> code.

Perhaps, but I use it twice (at least, more commonly three/four times) in every script, so the difference will soon mount up.
Quote
Author: Stefan
Posted: 4th March 2008 16:13
require_once() is still the best option imho to prevent, for instance, require-ing a class twice. Instead, make sure you have a good autoload() functionality to minimize the require_once() calls
Quote
Author: Michael Gauthier
Posted: 4th March 2008 16:19
When was that quotation from Rasmus made? I was under the impression that require_once() was slow up until 5.2.x.

Also, the above quotation states the *_once() calls are slow for an opcode cache. If you are not using an opcode cache it doesn't sound like there is a problem. If you're using an opcode cache and you are concerned about the speed of require_once() you should write a build script that concatenates all your scripts in the correct order (by reading require_once() calls) into a single file.

Not using require_once() sounds like a premature optimization to me.
Quote
Author: Richard Heyes
Posted: 4th March 2008 16:38
Michael Gauthier:
> When was that quotation from Rasmus made? I was
> under the impression that require_once() was
> slow up until 5.2.x.

IIRC (which is not at all likely) it was in his recent MVC article.

> If you are
> not using an opcode cache

You'd have to ask "Why?".

> If you're using an opcode
> cache and you are concerned about the speed of
> require_once() you should write a build script
> that concatenates all your scripts in the
> correct order (by reading require_once() calls)
> into a single file.

All I can say is "Yikes".

> Not using require_once() sounds like a
> premature optimization to me.

I don't think so. You'd have to have an oprganised require() structure, but I doubt it would be a problem.
Quote
Author: Matthew Weier O'Phinney
Posted: 4th March 2008 17:23
Richard, that article you quoted of Rasmus' is several years old. Additionally, as others have noted, a lot of work went into PHP 5.2.0 to create a realpath cache that makes the performance of require vs. require_once negligible even with opcode caches.

Please research issues like this before posting them -- no need to alarm the community unnecessarily.
Quote
Author: Q
Posted: 4th March 2008 19:36
*phew* thanks Matthew... alot

but even if require_once() remained negligibly slower why bother switching to require()

???

Code optimizations tend to be pedantic at best. Your time is better invested in configuring your environment, compressing output, caching etc.

Those are the quick wins which we'll make a significant impact on the performance of your web app

Is the extra millisecond for that code optimization really worth pecking thru your entire codebase ???
Quote
Author: Richard Heyes
Posted: 4th March 2008 21:28
Matthew Weier O'Phinney:
> Richard, that article you quoted of Rasmus' is
> several years old. Additionally, as others have
> noted, a lot of work went into PHP 5.2.0 to
> create a realpath cache that makes the
> performance of require vs. require_once
> negligible even with opcode caches.

Not everyone is using or can use PHP 5.2.x. Good to know it's not the case anymore though.
Quote
Author: Richard Heyes
Posted: 4th March 2008 21:52
> Code optimizations tend to be pedantic at best.
> Your time is better invested in configuring your
> environment, compressing output, caching etc.

Er, what?

> Is the extra millisecond for that code
> optimization really worth pecking thru your
> entire codebase ???

Well it could be as simple as a search/replace. And your app/website may well use require_once() a lot when it could do just as well with require(). So it could be worth it, particularly if you're not using 5.2.x. Individual circumstances apply.
Quote
Author: quintonparker@gmail.com
Posted: 5th March 2008 07:38
Richard Heyes:
> > Code optimizations tend to be pedantic at
> best.
> > Your time is better invested in configuring
> your
> > environment, compressing output, caching
> etc.
>
> Er, what?
>

All I'm saying is the are other areas in your app where you can safely optimize the app's overall performance without incurring changes to the codebase (which can and will result in bugs).

Safe optimizations such as opcode caching, output compression via zlib and mod_gzip for static elements, http caching etc blah. And if your app happens to be database-driven there's a dozen things you can do from optimizing table structures, ensuring queries hit indexes correctly and that they're not negating query cache etc. blah blah

All these optimizations occur outside of the app and would yield a greater return than code optimizations.

The language developers are working on the code optimizations so we don't have to. As we've just discovered the require_once() issue is no longer since php 5.2 +

I will say no more :-)
Quote
Author: Richard Heyes
Posted: 5th March 2008 10:43
> All I'm saying is the are other areas in your
> app where you can safely optimize the app's
> overall performance without incurring changes
> to the codebase (which can and will result in
> bugs).

I would half agree, but only if your code is already reasonable. If not, then I think it's an extremely valid area to make optimisations.

> All these optimizations occur outside of the
> app and would yield a greater return than code
> optimizations.

Not necessarily. The code could be quite poor for example and benefit greatly from optimisation.

> The language developers are working on the code
> optimizations so we don't have to.

That is so not true.
Quote
Author: quintonparker@gmail.com
Posted: 5th March 2008 10:53
I lied. This is now my very last comment...

okay Richard we disagree on various counts. I only commented on ur post bcoz other PHPers might follow what ur saying verbatim. Rafael certainly did

And so my opinions expressed are just alternate views for all PHPers to see and compare and decide for themselves how they wish to approach optimization
Quote
Author: Gaj Capuder
Posted: 5th March 2008 11:45
Richard Heyes:
> Well it could be as simple as a search/replace.
> And your app/website may well use require_once()
> a lot when it could do just as well with
> require(). So it could be worth it,
> particularly if you're not using 5.2.x.
> Individual circumstances apply.

Well that is not true. "require_once" has its own functionality specifics which are not the same as in "require". "require_once" could be used with a reason that there are actualy multiple calls for same file/class/script inside the app and if you replace it with just "require" you will most likely get an fatal error (duplicate delcarations of classes or functions).
Quote
Author: Philip Olson
Posted: 5th March 2008 18:26
Documentation is on its way, but if you really want a clue about your includes then try this extension:

http://pecl.php.net/inclued

Seriously, do it. Type 'pecl install inclued' and go to town. Search 'inclued' to find Gopal's blog entries for now, and it shows how to create stellar graphs. Do it.
Quote
Author: HM2K
Posted: 25th March 2008 01:34
Article is still found here:

http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html
Quote

Post Comment

Your name:
Your email:
(Don't worry, I won't spam you. Also, if you do put your email address in here, you'll get notified of new comments. If you don't, you won't.)
Comments:
  Do not post support type questions please

 
CAPTCHA image If you can't read the CAPTCHA then press the submit button to get another. Your comment will re-appear (as if by magic...).
Captcha image