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
Link to meIf 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!
Author: Richard Heyes
Posted: 4th March 2008 14:43 Rafael S. de Sá:
Quote> 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.
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.
QuoteThis 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.
Author: Richard Heyes
Posted: 4th March 2008 15:20 Jeremy Privett:
Quote> 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.
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.
QuoteAlso, 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.
Author: Richard Heyes
Posted: 4th March 2008 16:38 Michael Gauthier:
Quote> 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.
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.
QuotePlease research issues like this before posting them -- no need to alarm the community unnecessarily.
Author: Q
Posted: 4th March 2008 19:36 *phew* thanks Matthew... alot
Quotebut 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 ???
Author: Richard Heyes
Posted: 4th March 2008 21:28 Matthew Weier O'Phinney:
Quote> 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.
Author: Richard Heyes
Posted: 4th March 2008 21:52 > Code optimizations tend to be pedantic at best.
Quote> 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.
Author: quintonparker@gmail.com
Posted: 5th March 2008 07:38 Richard Heyes:
Quote> > 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 :-)
Author: Richard Heyes
Posted: 5th March 2008 10:43 > All I'm saying is the are other areas in your
Quote> 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.
Author: quintonparker@gmail.com
Posted: 5th March 2008 10:53 I lied. This is now my very last comment...
Quoteokay 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
Author: Gaj Capuder
Posted: 5th March 2008 11:45 Richard Heyes:
Quote> 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).
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:
Quotehttp://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.
Author: HM2K
Posted: 25th March 2008 01:34 Article is still found here:
Quotehttp://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html |

Comments
Posted: 4th March 2008 14:23