15

It is very common to use include files. I think it is overused to keep the codes tidy without considering performance. For several includes, disk should read the files, and as we extremely use disk, it can be a slow process. However, this is not the main slow process or the rate-limiting process, as loading the file with file_get_contents is few times faster.

I think this is the reason that major websites put javascripts within the html file rather than loading them by file. Alternatively, it can be a good idea to split a large JS file into several small JS files, as parallel http requests can load the entire JS codes faster. But this is different from php files, as php script reads include files one by one during the process.

  1. Please comment how much serious this problem can be? Imagine a webpage is loaded in 0.60s, can include of 10 php files turn it to 0.70s?

  2. Although this effect should be negligible, I want to know if there are approaches to speed up this process. I do not mean php caching like APC.

P.S. This question is not for practical application (a typical case), but theoretical considerations in general.

4
  • 1
    Using include should be extremely fast, and even faster if you enable APC. Your chasing the wrong end of the stick here. Commented Oct 25, 2011 at 6:15
  • Also, parallel request are normally slower due to the connection handling overhead. Simply combine all JS files and put them in your document root, then let your web server (Apache) handle the caching. Commented Oct 25, 2011 at 6:17
  • It could be very serious... if you're using machines from 70's :p How "fast" do you need the PHP code to run? Does 0.1s matter? How bad is it compared to highly reusable, flexible and extensible architecture? Commented Oct 25, 2011 at 6:23
  • 1
    Combining JS means less requests are needed, thus allowing other files to fill up the parallel download queue (I think the limit is generally 4), but there is an advantage to "chunking" (only combining up to a limit, eg 20-100KiB): the user can run some code before all of it has been downloaded. If someone is on a slow connection and your website is entirely unusable without a couple crucial JS files (which is a bad design in my mind), they'll appreciate being able to do something sooner. Commented Dec 16, 2016 at 21:54

5 Answers 5

19

include and its ilk is a necessity. It is similar to import in Java and python in that it is used for class and function definitions. include should be extremely fast, but using it will delay script execution compared to if it was not there. include is totally different from file_get_contents(). The latter is a function rather than a construct and returns a string. include will actually execute the code of the included file.

Your statement about splitting JS files is incorrect as script downloads from the same domain block parallel downloads and it's generally recommended to have as few includes as possible in general.

I highly doubt that having multiple includes, assuming all are necessary, is going to slow down the performance of your page. If you are having performance problems, look elsewhere.

If you want to speed up php, look into using a php compiler.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your informative answer. I know the difference of include and file_get_contents. By that comparison, I meant reading file from disk can be faster; then, the rate-limiting step is php processing of include.
+1 for quoting php compiler. I was always in doubt about php compiler (due to opposite opinions). Now I must try it :)
13

Consider this:

(index.php)
for ($i=0; $i<100000; $i++) {
    include('somefile.php');
}

(somefile.php)
<?php
// nothing here

index.php takes ~115 seconds (for me) to process 100,000 iterations while including somefile.php, even though somefile.php has nothing in it.

However:

(index.php)
for ($i=0; $i<100000; $i++) {
    // no file included this time
}

index.php now takes 0.002 seconds to complete without an include() construct.

(index.php)
for ($i=0; $i<100000; $i++) {
    echo $i .'<br/>';
}

index.php takes 0.02 seconds to echo 100,000 iterations of $i.

Of course, this is a pretty extreme example due to the large number of iterations, but it does show that by simply including an include construct, script execution times can be delayed quite exponentially. Consider this the next time you write a process with large numbers of iterations, ie. reading/writing large XML files, etc. It's best to keep your code inline, even if that means it's less "manageable". 'Cause not only are you adding ~115 seconds (~2 minutes) to script execution time at every ~100,000 iterations simply by including an include(), but consider if that include() (somefile.php) had processes of its own to execute. My example is simply adding an include() construct.. the included file contained nothing.

Now, including files here and there for a webpage, the times would be negligible. I was only pointing out that the include() construct does require extra processing regardless of its contents.

1 Comment

What if you put a parameter-based function in the include and used include_once() instead?
8

Yes, it does. The libraries you used to use will bring performance penalty due to a lot of includes underneath. The best approach to improve performance is:

  1. Put together all included files in a single one
  2. Use accelerator

It can speed up your solution by 22 times. Read more Here

Comments

4

PHP has to parse the code no matter if it is in the main php file or an include. Putting it in an include probably make no difference. Disk speed makes no difference either since it will be cached after the first time.

1 Comment

No, the file is cached by the OS (just the regular disk cache, PHP files fit in it very easily). APC would cache the result of parsing the file.
1

So, of course, including files may slow down the php script. Sometimes you need to include files to clarify your coding structure, architecture and design. But there is some methods and solutions to improve the speed of php script that has more includes.

Solutions

You can speed up your script by using these methods:

  • Changing your coding architecture and include some files when they are needed.
  • Using SSD hard disk and enough ram.
  • Adding all includes in one file. In my own way, I've added include file in a file called engine.php and I'm using it via including it by the router.
  • Caching data is another way to prevent data including.
  • You can change your compiler and use php 7.x compiler that is 10 times faster than php 5.x due to this reference. Some benchmarking test here shows that php 7.2 is faster than the HHVM compiler too.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.