Multiple includes bad for performance?

11

If I use too many includes on my page will it slow down or something like that?

    
asked by anonymous 07.05.2014 / 13:42

4 answers

13

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

The performance depends on the contents of your file that will be included, for example, many FOR cycles make the script slower, a database connection with a query badly built, etc. ..

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

Only the include function will not affect performance. Now if you compare a require () with an include () yes, require > is slower than include .

Use absolute paths when calling files, so php will not need to parse include_path .

Here are some examples of good practice for you that are starting:

  • php's manual will be your best friend and will answer (almost) all your questions.
  • Look for DRY (Do not Repeat Yourself) and KISS (Keep It Simple Stupid) practices, ie do not repeat code and keep everything stupidly simple.
  • Beauty is not fundamental, but a well-organized code makes system maintenance much easier.
  • Use comments, but remove them in the version used in production, comments are welcome but leave the heaviest archers (the next item completes this)
  • Minimizing files can be a good solution, since spaces and comments will be removed and some variables modified to improve performance and decrease file weight (in the case of javascript ), less traffic ).
  • 07.05.2014 / 13:51
    5

    When you make an include / require you obviously intend to include / require a resource from another external file, whether it is local or remote. This file (s) is stored on a disk which, in order to be read, depends on the file system itself, which is slow by definition. / p>

    With this little opinion you already know that yes , using too many includes / requires is bad for performance.

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

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

        
    07.05.2014 / 20:14
    4

    There is no difference between typing, for example, 15 lines of code in a file or playing 5 of those lines to another PHP file and giving an include in it.

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

        
    07.05.2014 / 13:51
    1

    An include can disrupt perfomance because you can make includes of unnecessary files at times of code you will not even use.

    You should study your system type 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 (or extend) them so you can simply pass on the values that will be handled by reference and / or parameter, but this will depend a lot the type of system, and the type of architecture you are adopting for development.

    Always look at design patterns and code management, so over time you will always be making more robust and less volu- ble and cleaner code that throughout your career you will have a good ready code library for use in later projects.

        
    22.02.2017 / 15:20