What's the Performance Cost of "Include" in PHP

What's the performance cost of include in PHP?

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...

Efficiency for including files of functions (in PHP)

Definitely separate them, for maintainability sake. I doubt performance will suffer at all, but even if it does (just a teensy bit) you're better off writing maintainable, readable code.

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.

Efficiency for including files of functions (in PHP)

Definitely separate them, for maintainability sake. I doubt performance will suffer at all, but even if it does (just a teensy bit) you're better off writing maintainable, readable code.

sending data into another php files cost

Generally its so subjective but for two simple function there is not much difference to implement them inside or separate them in different file if you are concerning about overhead in your app. you can find more details and statistic here.

But having clean code is much more effective, I believe.

You'd better follow famous design patterns and SRP to know where and how create classes and functions.

PHP optimization - include() or not

  1. The differences will be trivial.

  2. Don't repeat yourself. Do not put connection information in each file over and over again. Including sounds fine in your case.

  3. Stop making use of mysql_*(). Use PDO or MySQLi instead.

You're talking about micro-optimalisation, while it's probably better to start thinking about object oriented programming instead.

PHP : Does serialize cost much performance?

Serialize takes a long time for big things and a short time for little things. How big are your things and how fast do you need things to be?

You could directly measure the cost in microseconds of serialize() for your particular arrays with something like

$startTime = microtime(true);
yourSerializeFunction(); // <-- you are timing this
$endTime = microtime(true);
var_dump( $endTime - $startTime )

Or there are lots of other profiling options if you want to be professional about it.



Related Topics



Leave a reply



Submit