views:

312

answers:

4

Each page on my website is rendered using PHP.

Each PHP file uses around 10 includes. So for every page that is displayed, the server needs to fetch 10 files, in addition to the rest of it's functions (MySQL etc).

Should I combine them into a single include file? Will that make ANY difference to real world speed? It's not a trivial task as there would be a spaghetti of variable scope to sort out.

+14  A: 

Include files are processed on the server, so they're not "fetched" by the browser. The performance difference of using includes vs. copy and pasting the code or consolidating files is so negligible (and I'm guessing we're talking about in the 10ms to 100ms range, at the absolute most), it isn't at all worth it.

Feel free to include and require to your heart's content. Clean code is substantially more important than shaving < 100ms off a page load. If you're building something where timing is that critical, you shouldn't be using PHP anyway.

David Pfeffer
I think it's more complicated than that, cause it depends if the files are read from memory or disk. From disk - it can take several ms, depends if it's fragmented and the size of the file. (either the OS can cache the file, or the HDD device itself..)
Dor
@Dor don't underestimate disk cache abilities :) disk fragmentation will never affect your site
Col. Shrapnel
A: 

Although disk I/O operations among the biggest performance-eaters, a regular site won't notice any sensible number of includes. Before you hit any problems with includes, you probably already would have some opcode cache that eliminates this problem too.

Col. Shrapnel
A: 

include\ andrequires` open file only server side but that might be time consumming depending on the hardware/filesystem, etc.

anyway, if you can, use autoloader, only needed files will be loaded that way.

then if you think included files are a source of slowdown (and I think there is a lot of other point to look for improvement before), you can try to automatically merge file. you still have one file per class when developping but you can build file that contains each classes definition to have only one include (something like cat <all your included file>.php > to_include.php)

mathroc
+3  A: 

What takes time is figuring out where the files are actually located in the include path. If you got multiple locations in your include path, PHP will search each location until it either finds the file or fails (in which case it throws an error). That's why you should put the include path where most of the included files are to be found on top of the include path.

If you use absolute paths in your include path, PHP will cache the path in the realpath cache, but note that this gets stale very quickly. So yes, including ten files is potentially slower than including one large file, simply because PHP has to check the include path more often. However, unless your webserver is a really weak machine, ten files is not enough to make an impact. This gets only interesting when including hundreds of files or have many locations to search, in which case you should use an OpCode cache anyway.

Also note that when including files, it is not good practice to include each and every file right at the beginning, because you might be including files that are never called by your application for a specific request.


Reference

Gordon