Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Do web pages load faster or load slower when all headers, footers and basic functions are placed in external files and called using the statements below? Are there any other benefits to separating such content?

<?php include("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>                
<?php require_once("includes/functions.php"); ?>
share|improve this question
2  
Thanks for the edit Anna –  Anthony Jan 4 '12 at 18:58
    
No, I made a mistake in my original question, i did not mean to put include for the functions.php –  Anthony Jan 4 '12 at 19:09

1 Answer 1

up vote 6 down vote accepted

Do web pages load faster or load slower when all headers, footers and basic functions are placed in external files and called using the statements below?

There's no noticable performance issue. Check out the metrics in this blogpost, which are simple enough to recreate on your own. The results are:

------------------------------------
Running for 1000 files: require, require_once, include_once, include
------------------------------------
Results:
                     faster by   og. time
    include_once:          n/a   0.088193
    require_once: +   0.013137   0.101330
         require: +   0.030759   0.118952
         include: +   0.045299   0.133492 

The benchmark is more geared towards comparing the functions, but it's evident that the times are extremely small. This other benchmark shows:

[11-Jan-2008 11:31:57] Benchmark include 0.039840936660767
[11-Jan-2008 11:31:57] Benchmark require 0.05462384223938

Also for 1000 files. So apparently, the performance penalty is minimal. At heart both include(), require() and their _once() equivalents are simple filesystem reads, nothing special about them that would hint towards a performance concern.

Bear in mind that if you find includes to be slow, you might have stumbled upon a bug. The one I've linked to is closed, but you never know. A good hint is the first answer to the bug:

The include process is identical on all Win32 systems, any slowdowns are likely to be related to either the drive speed or the OS internals.

That said one of the reasons you are getting such bad numbers is because you are using a partial path PHP needs to resolve first. This compounded with ZTS mode makes things very slow, change the include statement to use full paths and you'll see much improved performance, not to mention much more consistent numbers.

It didn't make any difference with the bug, but you should always have it in mind, if you are going for thousands of includes.

Are there any other benefits to separating such content?

Yes, of course. You have one file for your functions, one for your header and one for your footer, which means that you don't have to rewrite them every time you need them. Isn't that enough? Don't repeat yourself.

share|improve this answer
    
+1 Thanks for your reply. I was wondering since there is no noticable performance issue, what if the website has thousands of pages? –  Anthony Jan 4 '12 at 19:08
1  
@01010011 - Do you mean you're includeing 1000's of pages, or the site simply has 1000's of pages? If you're includeing 1000's of pages, you probably need to re-evaluate your web app architecture. –  eykanal Jan 4 '12 at 19:14
    
Yeah, unfortunately vote ups require 15 reputation points and I have not reached there yet, i wish it was not like that –  Anthony Jan 4 '12 at 19:14
    
@eykanal, yes I meant that a website has thousands of pages and each page uses "include" to include the header, footer and function files. Just curious, is there a more efficient way to include parts of a web page like headers, footers and functions into thousand of pages? –  Anthony Jan 4 '12 at 19:19
1  
@01010011 I've updated the answer with some benchmarks for thousands files. There isn't a more efficient way to do what include does in php. If you include 1000 scripts in 1000 scripts, then the problem lies elsewhere... –  Yannis Rizos Jan 4 '12 at 19:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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