www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Disadvantages of include?

  1. #1
    Join Date
    Dec 2009
    Posts
    1

    Disadvantages of include?

    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.

  2. #2
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    There is no magic answer for this that works for everyone because it depends not just on the code but also the server architecture including memory, disk space, caching ability and so on.

    I suggest you run simple benchmarks on your code and determine if performance is being affected by too many includes or requires. One way is to start a simple script timer at the top of your code and end it at the bottom and output the script execution time in microseconds as demonstrated here in this nice how-to. There are also commercial packages such as Zend Optimizer, Zend Studio's Profile, Apache Benchmark tool and Pear has a benchmark package as well.

    But in terms of "include", specifically, here are general tips to optimization:


    • Include is more expensive than include_once
    • Use syntax: include '/path/to/file.php'; // single quotes, no parenthesis so PHP doesn't have to parse for variables if you don't need any

    Caching:

    Using include is less expensive for content retrieval than database queries, generally, but for templates and theming you can optimize them by utilizing caching, which actually uses both methods in many apps I've seen. Run benchmarks on a properly cached template based site vs. one that isn't and you'll be impressed, trust me. For caching techniques check out this excellent tutorial to create a template/theme caching class. Enjoy, and cheers.

    -jim
    Last edited by SrWebDeveloper; 12-02-2009 at 12:03 AM. Reason: fixed a broken link

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,918
    Quote Originally Posted by SrWebDeveloper View Post
    • Include is more expensive than include_once
    I have never tested it, but have read in a number of forum posts here and elsewhere that include/require_once has more overhead processing than include/require, since the *_once() functions have to do additional processing to check to see if the requested file has already been included. Of course, we're likely talking very small differences in actual time either way, and for me it's all hearsay at this point. Anyone have any definitive evidence on this? (Not that I'm really worried, just curious.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    I looked at what I wrote about includes being more expensive than include_once, and I think I got that b-b-backwards! Oops.

    Yeah, the differences are small when compared head to head. In a large template based system, for example, it might have a serious impact over time on a heavy load server. For most sites, it likely won't be an issue.

    My comments on the caching mechanisms really is the key to optimizing performance when includes are used more than sparingly, i.e. a template driven site where all pages require numerous includes (header, footer, content areas, nav bar, and so on.... i.e. *.tpl files)

    -jim

  5. #5
    Join Date
    Nov 2008
    Posts
    2,477
    I just ran a quick test as I was interested in quantifying this a bit.

    I made 10000 copies of a HTML file and included them all with the following script:

    PHP Code:
    <?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";

    ?>
    foo{0-9999}.htm are ~50KiB.

    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

    So as far as I can tell that are all pretty similar, being around 0.001 seconds per file. Obviously this would probably increase using PHP files, but it doesn't seem too intensive and probably not worth worrying about for a reasonable number of includes.

    I'm using PHP5.2 btw, don't know if these results would apply to older versions.
    Last edited by Mindzai; 12-02-2009 at 09:02 AM.

  6. #6
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    My assumption is that the combined indexing overhead of performing hundreds of include_once()s will still be far less than accidentally include()ing a single file twice unnecessarily--even with caching.

    In any case, I've seen fully functional sites running with include()s ranging into the 30's, 40's and quite possibly higher (many implicit include()s) with no ... significant performance issues. Though, my personal thought is that there is generally no need for more than about 10 ... maybe a few more.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  7. #7
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    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
    Wish I'd seen this before I posted.

    Anyway ... Did you do a pre-run to fill the OS cache? And do you have any other form of caching installed?
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  8. #8
    Join Date
    Nov 2008
    Posts
    2,477
    Quote Originally Posted by svidgen View Post
    Anyway ... Did you do a pre-run to fill the OS cache? And do you have any other form of caching installed?
    No other caching.

    I wouldn't have thought the OS file cache would have much effect, at least not much effect that wouldn't benefit each of the 4 functions equally. However by way of experiment I repeated the test with the following code, which deletes and re-creates files before each include:

    PHP Code:
    $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"
    include: 13.984048
    include_once: 14.384155
    require: 13.511622
    require_once: 14.449052

    Obviously this has an effect on the overall time, but I still don't see a significant difference. Maybe I'm missing something, but I find these results a bit surprising. Maybe 5.2 has been optimized enough for this not to be an issue? I know they did a lot in terms of optimizing.

  9. #9
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Thanks for posting the benchmarks, Mindzai. Interesting results and excellent analysis.

  10. #10
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    I wouldn't have thought the OS file cache would have much effect
    Yep. The OS keeps a cache of all "recently" opened files--kicks out the "oldest" file(s) from the cache when new files are opened. You also need to consider the disk cache--that's two levels of caching you need to consider when benchmarking. For an inactive disk, the difference between reading a small cached file and an uncached file can be orders of magnitude--1ms versus 10ms--multiples of this for an active disk.

    Your benchmarks show access times around 1ms--indicating they're probably being read from a cache somewhere (most disk seek times are between 5ms and 10ms).

    For the purposes of this benchmark, I think it would be most reasonable to "force" all of the files into the cache--I think we want to capture the cost of the include()s independently of disk access, since they probably will be cached during a live web request.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  11. #11
    Join Date
    Nov 2008
    Posts
    2,477
    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.

  12. #12
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Quote Originally Posted by Mindzai View Post
    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.
    Well, not necessarily. With a true disk access potentially costing 10 to 100 times that of a cache hit, even a single cache-miss can severely taint your results. You need to ensure that they are either all cache-hits or all cache-misses. Given the intention of the benchmark, it's more sensible to ensure that they are all cache-misses somehow.

    Although, seeing the results as they are, I think we can safely assume that you don't have to worry about a "lot" of include()s for most applications. Just be aware, that if you accidentally perform an include() multiple times on file(s) with significant payload, you're paying a lot more for the additional include()s than you would for the additional include_once()s.
    Last edited by svidgen; 12-02-2009 at 11:17 AM. Reason: removed a duplicate "somehow" ... and a few other typos ...
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  13. #13
    Join Date
    Nov 2008
    Posts
    2,477
    Yes I see your point svidgen. It's pretty safe to assume the files in benchmark 1 were all subject to os cache - the machine it was run on has about 4-5GiB RAM in use as cache most of the time with plenty to spare should the OS need more. The HDDs on-board cache I guess is around 32MiB, but like you say I can't say one way or the other if every file is in it or not though.

    The second benchmark should overcome this though as the files only exist right before they are included so they can't be cached.

  14. #14
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,918
    Generally the issue of doing multiple include/require when you don't need to and without getting a fatal error are in rather limited circumstances, such as a file that simply sets some application variables. If it defines functions or classes, then you're going to get a fatal error if you try to include/require it a second time, but if all it does is some sort of in-line processing, then you probably actually want to include it at each point.

    Anyway, thanks to Mindzai for doing those tests; and I'm going to continue to not worry about any performance difference and just use the one that makes logical sense for my application.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  15. #15
    Join Date
    Nov 2008
    Posts
    2,477
    Yeah that's my conclusion too. I think if you ever have to worry about performance caused by includes you've probably got bigger problems with your architecture anyway.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles