Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Question pretty much states it all, I am working on a large project where most calls to php include() between 100 and 150 files. On average the time php takes is between 150 and 300 ms. I'm wondering how much of this is due to including PHP scripts? I've been thinking about running a script that checks most accessed files for particular calls and merge them into one file to speed things up, but for all I know this has zero impact.

I should note that I use APC, I'm not fully aware of what APC does in the background, but I would imagine it might already cache my files somehow so the amount of files doens't really make a big difference?

Would appreciate any input on the subject.

Of course, 300ms isnt much, but if I can bring it down to say, 100 or even 50ms, thats a significant boost.

Edit:

To clarify I am talking about file loading by php include / require.

like image 490
Naatan Avatar asked Jun 15 '11 15:06

Naatan


People also ask

Do PHP includes slow down?

Yes it will slowdown if you are including the script from other servers.

Does file size affect performance?

Disabling or setting the page file size too small can reduce system performance and cause instability and crashes in Windows.

Where should I put my PHP files?

If your server supports PHP, then you do not need to do anything. Just create your . php files, put them in your web directory and the server will automatically parse them for you. There is no need to compile anything nor do you need to install any extra tools.

What is a PHP file?

So what exactly is a PHP file? Generally speaking, a PHP file is a plain-text file which contains code written in the PHP programming language. Since PHP is a server-side (back-end) scripting language, the code written in the PHP file is executed on the server.


1 Answers

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.

like image 174
Andrew Curioso Avatar answered Sep 25 '22 13:09

Andrew Curioso