1

I am developing an application where I have a requirement that I have to include multiple files, as follows:

File1.php will include File2.php

File2.php will include File3.php

File3.php will include File4.php

Now, what will be the best and optimized way of doing these. I tried with include() but I think its slowing down my server as my server load graphs goes approx 3-5 times higher than normal server load.

These all files are heave means arround 50 KB for each file. So can anyone please suggest me what will be the best option to include these files so that it will not affect the speed of the site,

Which will be best?

require()

require_once()

include()

include_once()

OR if anyone has some other option then please tell me,

Thanks

8 Answers 8

3

The difference between include() and require() is whether you get a warning or a fatal error (respectively). The PHP docs don't mention any performance difference.

Using the _once() variants is useful if you want to avoid duplicating the contents of the included files. From the PHP docs for include_once:

include_once() may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.

2

You should reorganize your files. Have a "common.php" file that includes all the needed files. You shouldn't have a chain like that.

I sincerely doubt a couple of includes are slowing down your server. Most web applications have tens, maybe even hundreds of PHP files. There is no way 3 extra files will slow down your application that much.

Also, the speed difference between the functions you mentioned is negligible, although you should use include_once/require_once instead of their counterparts, to make sure you don't include the same file multiple times.

4
  • I think file organization is OK, caz if I'll use common.php to include other files then, File1 will include common.php and the common.php will get the reuiqred file. Almost same I am already doing ....
    – djmzfKnm
    May 21, 2009 at 5:11
  • Everywhere on net I have read that "include_once/require_once" are slower than "include/require" and reuire is slower than include ?? I am not getting exactly who is write and who is wrong ???
    – djmzfKnm
    May 21, 2009 at 5:12
  • @Parshant: Yeah, but my way will be way more organized and it will be more maintainable in the future. Honestly, stop worrying about the very small speed difference between the functions. 0.000002 seconds slower won't do you any harm. ;) May 21, 2009 at 5:17
  • 1
    @Prashant: If in doubt, measure it yourself. In the real world, the speed difference between those 4 functions is negligible - the real performance bottlenecks are elsewhere and several orders of magnitude more serious (again, performance measuring is a necessity here). May 21, 2009 at 5:53
2

Jusn a note to know. In one of my projects I had to implement a kind of factory for lots of classes (about 50). I created a file for each class and including them was dramatically slow. I decided to join them into one file. This incereased performance very appreciable.

I think it is related to file system slowdowns when there are many entries... Not sure why it's so, but fact - include of 1 big file much faster then 50 small.

2
  • True, but the difference between 1 file and 4 will be much smaller. May 21, 2009 at 22:43
  • Also true, but when you use 4 instead of 1 and do this 10 times in a project... 4*10 = 40 instead of 10... Well, this is just a note and, posiibly, offtopic. I only mean "keep this issue in mind".
    – Jet
    May 22, 2009 at 8:40
2

An article on Tech Your Universe talks about the require vs require_once issue. In essense, when doing absolute paths require() is definitely faster than require_once() and include() is definitely faster than include_once(). Their exact claim when they replaced the once calls with the sans-once calls:

First, strace revealed that there were 2848 syscalls to serve a page, down from 4782, (-40%). Next I went to ab for more testing, and found that the average page request time went down to 36.5, from 46.5ms (-22%), and the server was able to serve 27.1 requests per second, up from 21.4 (+%22) .

According to comment 90017 by Konstantin Rozinov on the require_once page on php.net, the difference between require() and require_once() is tiny and only makes a difference on large web applications. I would personally like a definition of large, but I think we get the idea. The commentator also claims that "when using APC opcode caching, the speed difference between the two is completely irrelevant." Regarding the actual implementation of require and require_once: "This indicates that either the OS (Ubuntu Linux in my case), or Apache is "caching" or knows the results of the previous stat() calls, so it doesn't bother repeating them." Take that for what it is worth.

My personal summation:

  • If you are going to use require_once, try to use absolute paths.
  • Be sure to use opcode caching of some sort.
0
0

I prefer splitting everything into as many files as it’s convenient for me to develop, but then flatten it all into one file before uploading it to server.

PHP string and regex functions make it super easy to write a script that will replace any include 'somefile.php'; with actual contents of that file (stripping < ? ? > if necessary).

1
  • Have you measured the speedup from doing that? I'd like to know if that has any visible effect on performance. May 21, 2009 at 5:10
0

Definitely choose include() or require() over the '*_once()' variants. You probably need some sort of opcode cache to improve your performance.

0

Your answer will depend on your need, mostly. Points to keep in mind are:

  1. include is faster than require.
  2. if a file is "included" but is not found, it does not result in a fatal error. The script will continue running.
  3. if a file is "requried" but is not found, the script execution stops instantly.

However, a side note: if your application is including file4 in file3, file3 in file2 and file2 in file1, then I'd say you should reconsider your file organisation. I'm sure you can come up with a better way of organising your code into files.

ADDITION: The speed difference between include and require is pretty marginal. However, require_once() is significantly heavier than require(). Likewise for include_once() and include().

cheers,

jrh

4
  • 4
    Do you have a source for the speed differences between include and require? I would like to see that tested somewhere. May 21, 2009 at 5:03
  • Either way, the speed difference will be a couple microseconds; nothing worth fretting over. May 21, 2009 at 5:10
  • Well I don't have the source code. But I distinctly remember reading it in some documentation. But yea, the couple microseconds difference is hardly worth fretting over.
    – jrharshath
    May 21, 2009 at 5:43
  • include() and require() both execute the same code. Only in case of an (file open) error there is a if(type==require) { message, bailout } else { message } ( see zend/zend_language_scanner.l, compile_file() )
    – VolkerK
    May 21, 2009 at 7:55
0

I would suggest using xdebug PHP extention to benchmark and profile your script. It will tell you how long each function took to execute. You can find more information on how to install it and what it can do here: http://www.xdebug.org/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.