This is a discussion on including files within the PHP General forums, part of the PHP Programming Forums category; hi everyone I've read that require_once is really slow. However, I'm not quite sure *why* it is slow.. ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
hi everyone
I've read that require_once is really slow. However, I'm not quite sure *why* it is slow.. surely it is setting a variable in memory somewhere, and everytime it wishes to include a file, it checks whether that variable is set?! This to me doesn't seem like an expensive operation! In the case that require_once is prohibitively slow, are there any other tactics in ensuring that files only get included once? PHPBB uses a tactic where their include files don't actually pull in any dependent files; they depend on whatever is including them to have included the relevant files. I don't really like this option as it doesn't explicitly state the dependencies an include file may have. I was thinking of putting in C++ style include guards, but I'm thinking this is how PHP would implement the require_once function! Thanks Taras |
|
|||
|
Taras_96 schreef:
> hi everyone > > I've read that require_once is really slow. However, I'm not quite > sure *why* it is slow.. surely it is setting a variable in memory > somewhere, and everytime it wishes to include a file, it checks > whether that variable is set?! This to me doesn't seem like an > expensive operation! It is not slow. Who told you that nonsense? I use it all the time in all my sites without ever noticing any perceivable slowdown. Are you actually having performanceproblem because of including_once a file? Of course, when the included file is huge, it takes PHP more time to process it, but the same goes for all PHP files. > > In the case that require_once is prohibitively slow, are there any > other tactics in ensuring that files only get included once? PHPBB > uses a tactic where their include files don't actually pull in any > dependent files; they depend on whatever is including them to have > included the relevant files. I don't really like this option as it > doesn't explicitly state the dependencies an include file may have. Don't get into that mess. Simply use include_once() and you will NOT run into any problems. > > I was thinking of putting in C++ style include guards, but I'm > thinking this is how PHP would implement the require_once function! Maybe it does, not sure. > > Thanks > > Taras Don't fix it when it ain't broke. Don't believe anything somebody wrote on the net. You can simply benchmark the include_once() yourself easily with microtime() and you'll probably find you don't have a problem. Regards, Erwin Moller |
|
|||
|
Hi there Erwin,
As you guessed, I'm mostly going off what people have written on the web, such as http://uk2.php.net/manual/en/functio...once.php#62838. I'm just starting out at the beginning of my project, so I'm trying to establish the best file structure to use. The best way is to use require_once, so that you can see what dependencies a library class has (on other files/classes). Obviously if you are using require or include, then you can't do this as it runs the great risk of trying to include a file twice. The fact that the phpBB software (which I assume, maybe incorrectly, was written by people that know their PHP) goes to some lengths to avoid the require_once construct implies that maybe there is a shred of truth to require_once being slow. There are no performance problems yet, but if our library code depends on this construct, and later on we write some pages that experience a very heavy load and are slow because of our use of require_once, then we'd be forced to change the entire structure of our library, which is a very very bad thing ;) Taras |
|
|||
|
On Jul 15, 6:28*am, Taras_96 <taras...@gmail.com> wrote:
> I've read that require_once is really slow. Really slow? No. It's a tiny bit slower than require(). Testing with Apache ab, you'll notice a small reduction in requests/sec. > other tactics in ensuring that files only get included once? Follow the one-class-per-file and PEAR directory layout conventions. Then you can rely on autoloading, or using something like this (in a utility class): // e.g.: loadClass('Foo_Bar_Crunch') function loadClass($class_) { if (class_exists($class_, false) || interface_exists($class_, false)) { return; } require str_replace('_', DIRECTORY_SEPARATOR, $class_) . '.php'; } Keep in mind the performance of require_once vs. autoload and the function above may depend on your environment; it may not be worth the hassle to avoid it. Steve |