Is multiple includes bad for performance?

Asked

Viewed 913 times

11

If I use many includes on my page will make it slower or something?

  • The answer is very broad, it depends more on what you are including than on the amount of includes. You might make one include that leaves a slower appellation than doing 15 includes different.

4 answers

13


The include does not slow down the page, but rather the contents of the file that will be included.

Performance depends on the contents of your file that will be included, for example, many FOR cycles make the script slow down, a connection to the database with a query evil was built, etc...

The best would be to look for how to improve the performance of your php script, best development practices, etc...

Function only include will not affect performance. Now compare a require() with a include() yes, the require is slower in relation to the include.

Use absolute paths when calling files, so php won’t need to parse include_path.

I leave here some examples of good practice for you who are starting:

  1. The manual php will be your best friend and will answer (almost) all your questions.
  2. Look for practices of DRY(Don’t Repeat Yourself) and KISS(Keep It Simple Stupid), I mean, don’t repeat code and keep it all stupidly simple.
  3. Beauty is not fundamental, but a well organized code makes system maintenance much easier.
  4. Use comments, but remove them in the version used in production, comments are welcome but make the arches heavier (the next item completes this)
  5. Minifying files can be a good solution, since spaces and comments will be removed and some variables modified to improve performance and decrease the weight of the file (in the case of javascript, less traffic).
  • All you had to talk about was Autoload ;)

  • 1

    Just one comment, on all the tests I’ve done require and the include has virtually the same performance, the only thing that has changed is if the included file is inaccessible in the case of include issue only one error notification, already in require it terminates execution if such file is inaccessible. Perhaps the require only make any difference with the include in case of performance if you try to include more than 100 files (theory only), in case of import of important libraries always make use of require_once.

  • @Guilhermenascimento then, "practically" indicates a difference, though little. As I explained above, including 1 poorly made script may be slower than inserting 100 includes with proper performance.

  • 1

    Sorry I seem to be disagreeing, but I see so, if a script using "include" is poorly made, then it will be bad without include or with include, this is not the difference factor. I’m not trying to fight you, I’m just saying sometimes there’s no difference. require and include are not functions, the only difference between them is the type of error that will issue, already compare include with include_once or require with require_once then I would agree with you (although the difference is insignificant). See you later :)

  • I don’t think it’s a good practice considering that a PHP project has many source files, so the job of doing it will be for a totally derisory result compared to the same.

  • @Fabiowilliamconception for this there are tools that help in the task, for example, the Gulp. In my projects to minify CSS and JS files that will be sent to production, I have the Gulp configured and just one command to be ready for production, including changing the references in the respective files.

Show 1 more comment

5

When doing a include/require you obviously intend to include/require a resource from another external file, whether(m) it(s) local(s) or remote(s). This file(s) is(are) stored(s) on a disk that, to be(s) read(s), depends(m) on the filesystem which, by itself, is slow by definition.

With that little look you already know yes, use many includes/requires is bad for performance.

But there is another very significant factor against multi-file fractionation, which is the interpretation of written code. If you include 15 files in a single to be accessed by given Request, the interpreter will "read" and interpret all tokens and parse the syntax of 15 files before to allow the server to send a response such as output pro browser.

This not only increases the time between a request and another, but also makes any debugging an error task Herculean for the developer.

  • Does PHP actually read and compile the PHP file each time it is requested? I really believe there must be some system cache to optimize this.

  • 2

    If memory serves, until version 5.4 all caching should be done by the programmer, whether with APC or another engine. Starting with PHP 5.5, APC became obsolete FOR PHP 5.5, since from this version the type of cahing that APC provided is natively made.

  • I left an answer for you below, hope I can help you in your doubt.

4

There is no difference between you writing, for example, 15 lines of code in a file or throwing 5 of these lines to another PHP file and giving it a include.

In some cases you’ll want to use include_once to ensure that the same file is not included 2 (or more) times (see).

  • In fact there is a difference yes, because when you give include in a file you bring all that source code that is in it to the source file, then just in case you will not use even 1/3 of that the interpreted will compile in the same way, soon losing performance. In this case it is best to use classes and only work with instances and then delete them if you will not use them anymore.

1

A include can disrupt performance because you can make unnecessary file includes at times of code that you won’t even use.

The guideline is to study your type of system well and if you have variables in common with solid values use them as constants (if the value is immutable).

But if you have variables / methods in common with mutable values, create classes and inherit them (or extend them) so you can simply pass on the values that will be manipulated by reference and / or parameter, but this will depend a lot on the type of system and the type of architecture you are adopting for development.

Always try to study project Patterns and code management, so over time you will always be making more robust and less voluble and clean codes that throughout your career you will have a good code library ready to use in later projects.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.