PHP Include(): File Size & Performance

PHP include(): File size & performance

There will be a difference, between a 200KB and a 20KB file... But you will probably not notice it : a 200KB file is not that big -- and you generally use a lot of files that are not "small", when you're building a big application.

There are two things that take time, when you're loading a .php file :

  • The PHP source code is "compiled" to "opcodes" -- that's quite equivalent to JAVA bytecode
    • This is done each time a PHP file is included, by default
    • But, using some opcode cache like APC, those opcodes can be kept in memory, and this compilation stuff not done each time anymore -- which is great : it'll mean less CPU used, as the compilation will not be done anymore (it'll be done only once in a while).
  • The opcodes are executed

    • Depending on what you script contains, this can take some time, or not :
    • If the file only contain functions or classes definitions, this will not take much time : nothing will get executed.
    • If the file contains instructions, it'll take more time ^^


As a sidnote : in a general situation, you'll gain a lot more time/cpu/resources optimizing your SQL queries, or adding some caching mecanism, than thinking about that kind of stuff.

Does reading include files slow down php script load?

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.

How big of an impact does including PHP files have on performance?

File loading is a tricky thing. As others have said, the only sure fire way to tell is to do some benchmarks. However, here are some general rules that apply only to PHP loading, not files with fopen:

  • APC will store its opcode cache in shared memory so you will take a hit on the first load but not subsequent loads.
  • include and include_once (and their require cousins) are actually quite heavy. Here are some tips to improve their speed:
    • Use absolute paths to your files (avoid relative paths like ../foo.php)
    • Both the _once functions need to check to make sure that the file wasn't also included via a symbolic link since a symbolic link can produce multiple paths to the same file. This is extremely expensive. (see next point)
  • It is much cheaper to load only the files you need than to call include. Make use of auto-loaders to only load classes when they are needed.
  • Local disks will almost always be a better bet than networked storage. When possible, if you have multiple servers, keep copies of the source code on each server. It means you need to update multiple places during a release but it is worth the effort in performance.

Overall it is dependent on your hard disk speed. But compared to not loading a file at all or loading it from RAM, file loading is incredible slow.

I hope that helped.

When is a php include file parsed?

Files are included if and when the include statement is reached at runtime. To very succinctly summarise what that means, the following file is never going to be included:

if (false) {
include 'foo.php';
}

PHP included file and performance impact of filesystem

My understanding of include(), require(), and the like is that it works a lot like the C preprocessor #include directive and basically runs all that code as if it were inline in the current file at that position, as you believe.

As some of the above comments have said, if those files are being frequently used (e.g. constantly called via include() ), they are likely sitting in RAM or at least a disk cache.

It's worth nothing that PHP files are essentialy JIT compiled and cached anyway, so you shouldn't notice a performance hit either way. (More detailed info here).

Also, as a sidenote - include_once() and require_once() do have a significant overhead when compared to include() and require(), so if speed is a factor, try to avoid the use of those calls.

Include one big file or several smaller ones?

The difference when including one big file or just parts of the same file multiple times will be negligible to non-existent on local server (via local filesystem).

You also mention including files from remote server, then including multiple will make a difference over one, as for every include the server will make a separate request and that will bring extra overhead.

Also, if you expect to include .php source files over remote, then it will not workas .php files are parsed on the server side, so you will only be able to include the output of parsed .php files on remote server.

Website & PHP: Which performs faster. Load files from few directory vs. many directories?

Assuming you're using a UNIX-based OS there will be very little difference so you should use what you find easier to maintain. FTP is an entirely different case as it actually transverses directories as a human would (it doesn't have access to your inodes).

Because of how inodes work, your operating system is not going to transverse your directories one by one looking for a reference to another file. Directories exist to make your life easier but most filesystems do not represent them internally as anything more than an organizational file.

You will gain filesystem performance boost by enabling dir_index (instructions) on your extX filesystem (or alternatively, check out XFS as it's really good when dealing with large numbers of files), regularly cleaning out files and defragmenting the disk and using faster drives.

Also, try to use require_once() rather than require() when loading files, as this way the file will only be loaded a single time.



Related Topics



Leave a reply



Submit