Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the performance cost of "include" in PHP?

Tags:

Just wondering if anyone has information on what "costs" are associated with including a LARGE (600K or more) php file containing 100s of class files. Does it really make much difference in comparison to autoloading individual files that for instance searches across several directories before finding a match?

Would having APC caching on make this cost negligible?

like image 525
FilmJ Avatar asked Feb 03 '11 19:02

FilmJ


People also ask

What does include () do in PHP?

The Include() function is used to put data of one PHP file into another PHP file. If errors occur then the include() function produces a warning but does not stop the execution of the script i.e. the script will continue to execute.

Do PHP includes slow down?

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

What is difference between require and include in PHP?

The require() function will stop the execution of the script when an error occurs. The include() function does not give a fatal error. The include() function is mostly used when the file is not required and the application should continue to execute its process when the file is not found.


1 Answers

Basically, the cost of including one big file depend on your usecase. Let's say you have a large file with 200 classes.

If you only use 1 class, including the large file will be more expensive than including a small class file for that individual class.

If you use all 200 classes, including the large file will be significantly less expensive than including 200 small files.

Where the cutoff lies is really system dependent. I would imaging that it would be somewhere around the 50% mark (where if you're using less than 100 classes in any one request, autoload).

And using APC will likely shift the breakeven point closer to less classes (so without, 100 classes used might be the breakeven point, but with it might be at 50 classes used) since it makes the large single include much cheaper, but only lowers the overhead of each individual smaller include slightly.

The exact break-even points will be 100% system dependent (how fast is your disk I/O, how fast are your processors, how much memory, etc). So the only way to know for sure on your platform is to test.

However, more is at stake than raw performance. Maintainability will suffer with one large file since it's harder to work on multiple classes at the same time (tabs in an IDE become useless). I personally would keep all the classes in separate files and make my life as the developer easier rather than making one giant monstrosity of a file.

Now, if you have facebook traffic levels, it may be worth investigating further. But if you're not, I personally wouldn't worry about it...

like image 54
ircmaxell Avatar answered Sep 28 '22 10:09

ircmaxell