I am now writing a php framework. I am wondering whether it will slow down when php require
/include
or require_once
/include_once
too many files during a request?
-
Possibly a duplicate, check these posts. http://stackoverflow.com/questions/3589117/using-too-many-php-includes-a-bad-idea and http://stackoverflow.com/questions/2198253/will-including-unnecessary-php-files-slow-down-website – verisimilitude May 18 '12 at 06:24
4 Answers
Well of course it will. Doing anything too many times will cause a slow down.
On a more serious note though, IO operations that touch disk are very slow compared to anything that happens in memory. So often times, including files will be a major performance factor when using a large framework (just look at Zend Framework...).
However, there are typically ways to alleviate this such as APC and similar op code caches.
Sometimes programming approaches are also taken. For example, if I remember correctly, Doctrine 1 has the capability to bundle everything into 1 giant file as to have fewer IO calls.
If in doubt, do some indepth profiling of an application written with your framework and see if include/require/etc are one of the major slow points.

- 33,060
- 6
- 68
- 78
Yes, this will slow your application down. *_once
calls are generally more expensive, since it must be checked whether that file has already been included. With a lot of includes, there is a lot of hard disk access and a lot of memory usage bundled. I've developed applications with the Zend Framework that include a total of 150 to 200 files at each request - you really can see the impact that has on the overall performance.

- 26,516
- 9
- 93
- 110
-
1This benchmark suggests there is no noticeable difference in the speed between `require` and `require_once`: http://www.php.net/manual/en/function.require-once.php#90017 – Kaivosukeltaja May 18 '12 at 06:28
-
no noticable difference != no difference. it could be noticable if the benchmark is done on a framework (with include/require inside another include/require) and initiated by enough number of requests. – LeleDumbo May 18 '12 at 06:33
-
The main issue however is that you normally will have many `require_once` calls at the top of each file, including all the required classes. So there may be like 20 attempts to load a specific file, and 19 of those will result in being just overhead. – Niko May 18 '12 at 06:37
-
@LeleDumbo: 0.000002 seconds of difference for 800 calls is close enough for "no difference" for me as it can just as well be chalked up to metering inaccuracy. When using relative paths, `require_once` was even marginally faster. @Niko: unnecessary includes can of course slow the system down but that doesn't mean `require_once` is a slower function than `require`. I'm pretty sure that the `_once` check doesn't need to access the file system. – Kaivosukeltaja May 18 '12 at 07:36
The more files you include will add to some load. However, if you have to choose between require and require_once, require_once / include_once take more load because a check will need to be done by the server to see if the same file has been included elsewhere. So if you could possibly avoid that, at least you could boost performance.

- 532
- 6
- 12