Menu
I have a habit of splitting my contents in separate file and include them in main script. It helps to manage. But,I was wondering is there any disadvantage of using include()? How many files can you include in a script without hampering the server/client side performance?
Thanks in advance.
[LIST]
[*]Include is more expensive than include_once
[/LIST]
[/QUOTE]
[code=php]
<?php
$start = microtime(true);
for ($i=0; $i<10000; $i++) {
ob_start();
include "/var/www/test/files/foo$i.htm";
ob_end_clean();
}
$end = microtime(true);
$time = $end - $start;
print "included $i files in {$time}s";
?>
[/code]
I ran the script with include, include_once, require and require_once 5 times each and took an average of the results:
include: 10.099652s
include_once: 10.303111s
require: 10.257192s
require_once: 10.240087s[/QUOTE]
Anyway ... Did you do a pre-run to fill the OS cache? And do you have any other form of caching installed?[/QUOTE]
[code=php]
$path = '/var/www/test';
if (sizeof(glob("$path/files/*")) > 0) {
for ($i=0; $i<10000; $i++) {
unlink("$path/files/foo$i.htm");
}
}
$start = microtime(true);
for ($i=0; $i<10000; $i++) {
copy("$path/foo.htm", "$path/files/foo$i.htm");
ob_start();
require_once "$path/files/foo$i.htm";
ob_end_clean();
}
$end = microtime(true);
$time = $end - $start;
print "included $i files in {$time}s";
[/code]
I wouldn't have thought the OS file cache would have much effect[/QUOTE]
Yes, but surely that would apply equally to all of the functions. It would opnly make a difference if it was enabled for some tests and not others I'd have though? I thought what we are interested in here is the differences between include, include_once, require and require_once? Surely caching or not, the same conditions exist for all of them. I was under the impression any difference would come from PHP's processing, for example examining symbol tables etc for the *_once family.[/QUOTE]
Well, I think the design for includes is--and should--be with the idea that they [I]generally[/I] do not directly perform any operations--but reveal functionality. That is, they ought to define functions, classes, and possibly variables. So, you wouldn't want PHP to re-include these things. But, you would also want to avoid including unused functionality--which warrants conditional includes and the possibility that a particular include is called multiple times.
IMHO, every include should be [B]include_[I]once[/I]() [/B]-able.[/QUOTE]
Of course, autoloading generally just a conditional [B]require_once()[/B] anyway ...
... did we answer the initial question here?[/QUOTE]
I haven't done any real timings on it but it loads as fast as most any other website commercial or otherwise.[/QUOTE]