Page 1 of 2
How many include/reuire , and its effect on PHP script speed
Posted: Wed Aug 08, 2007 3:45 am
by niro121
Hi friends,
Hmmm this time i have a practical question. Hmmmmm so far my php applications (mostly sites) had only few "includes" like two or three classes in each. Now in my new app im having a very nice complex object structure and will have about 6 classes. So i have to included them in each and every page. One class is written in one php file and each contains about 200 lines. Hmmm Now i need to know what will happen to the speed when we include so many include files to a page.
Now i know in other languages like Java "import" is something like "include/require" in php. Am i correct? In java or C# we call so many classes and libraries from import we call. Right, now i need to know is this system suitable for PHP. For all those PHP people comment how many complex and bulky classes you use in PHP. Hmmm... i know its hard to say how many lines of code is ok... I wont need that.. Will find it with experience. I want to know when we call lots of include files will it delay the scripts. Hmmm.... whats your thoughts?
Posted: Wed Aug 08, 2007 5:56 am
by Arawn
Right, now i need to know is this system suitable for PHP. For all those PHP people comment how many complex and bulky classes you use in PHP. Hmmm... i know its hard to say how many lines of code is ok... I wont need that.. Will find it with experience. I want to know when we call lots of include files will it delay the scripts. Hmmm.... whats your thoughts?
I tend to go with the likes of Lerdorf and Schlossnagle (George) who recommend between 5 and 10 includes. I think the number they suggest are arbitrary and the important thing is to pick a number that works for your production environment. I'm even more conservative than Lerdorf (at 5 includes) because almost all of my clients are on shared servers - no opcode cache and constant load.
From George Schlossnagle:
A reasonable number of includes is fine (and promotes good code organization), but 25, 50, 100+ will impose a real performance penalty.
Posted: Wed Aug 08, 2007 8:58 am
by niro121
Yea thats really great to here.

Posted: Wed Aug 08, 2007 10:06 am
by Begby
You might want to look into __autoload() so you don't have to include files in a page where the file is never called.
I am not sure of how many files I include, but with Zend Framework and my project I have many hundreds of class files. I am thinking its gotta be at least 50 files getting included on every page load. I have yet to see a big performance hit with that many even under load.
Re: How many include/reuire , and its effect on PHP script s
Posted: Wed Aug 08, 2007 11:12 am
by ev0l
A project I have been working on,
Phaux, has almost 300 files. Performance is not a problem. If performance becomes a problem you might want to look at installing an opt-code cacher, like APC. In fact on a production server I highly recommend installing APC even if you are not seeing any performance problems.
Like I said to you in a different thread. Don't worry about performance until you start to see issues. Focus on clean, simple, and modular design. If your design is clear and simple performance bottlenecks will be easy to locate and fix.
Posted: Wed Aug 08, 2007 1:59 pm
by s.dot
Yeah, don't get into pre-optimisation. Design what you need with as many files as you need. As long as the logic is clear, and your coding is clear, when a situation DOES arise, you'll be able to locate it pretty quickly.
Posted: Wed Aug 08, 2007 4:30 pm
by Christopher
The real point in questions like this niro121 is: do you have a performance problem. If you do not then do not let hypothetical performance problems influence your design. Six files of 200 lines sounds like you are on the right track. I occasionally check my sites to see how many includes they use and it is usually around 15, but that is using a controller framework.
Posted: Wed Aug 08, 2007 4:49 pm
by superdezign
All of my inclusions are basically strung from one file to another, so including one file basically includes three more, each which include about 3 more. It's usually a total of about 10 - 20, but there's no real performance hit. If anything, includes are the least of your worries.
Posted: Wed Aug 08, 2007 5:40 pm
by Ollie Saunders
It is thanks to responses like these why DevNet is the best PHP forum.
Posted: Thu Aug 09, 2007 6:43 am
by Arawn
It's interesting that most here seem to see this as premature optimization while I'm sure, at least, Schlossnagle actually referred to the number of includes as a (software quality) metric. That's definitely two different points of views.
Posted: Thu Aug 09, 2007 7:43 am
by timvw
Afaik, in the
first presentation i found by mentionned Schlossnagle i read the following:
Control your include trees to no more than 10 includes.
(Yes, that number is made-up.)
Which, to me, seems an acknowledgment that caring about their number, could be premature optimization...
Posted: Thu Aug 09, 2007 10:53 am
by Arawn
timvw wrote:Afaik, in the
first presentation i found by mentionned Schlossnagle i read the following:
Control your include trees to no more than 10 includes.
(Yes, that number is made-up.)
Which, to me, seems an acknowledgment that caring about their number, could be premature optimization...
I find it interesting that they are written off so easily. Personally, for their work on PHP, APC, and some really large websites, I'm will to believe Lerdorf and Schlossnagle know what they're talking about when it comes to includes. They work on PHP and APC and I would think they know something of the inner workings of these and have the experience of using them in really large projects. I'm also sure they know what is premature optimization, Schlossnagle, in the link you provided, speak of premature optimization twice.
Posted: Thu Aug 09, 2007 12:23 pm
by ev0l
Arawn wrote:
I find it interesting that they are written off so easily.
I don't think anyone is writing them off. If you work for Yahoo and your application gets millions of hits a day then having a small number of includes might help the performance of your application by a few microseconds. But way sacrifice code reusability, modularity, ease of development, design, and good practice for a few milliseconds on an application that does not gain much from the speed improvement.
The linked to slides even point out how most applications are database bound anyway and improving 10% of your code by %20 is likely going to make no real improvement at all. Even if you have thousands of includes your database performance is likely to be three or four times worse than your application performance.
All of this is moot anyway. It would be trivial to create a deployment script that compresses thousands of include files into a handful of files but I would never want to develop the application that way. (Class Foobar is in file A-H.php on line 3,468 ... Yikes!!).
Posted: Thu Aug 09, 2007 12:55 pm
by Christopher
Arawn wrote:I find it interesting that they are written off so easily. Personally, for their work on PHP, APC, and some really large websites, I'm will to believe Lerdorf and Schlossnagle know what they're talking about when it comes to includes. They work on PHP and APC and I would think they know something of the inner workings of these and have the experience of using them in really large projects. I'm also sure they know what is premature optimization, Schlossnagle, in the link you provided, speak of premature optimization twice.
I don't think the people were written off, it was it OP basing a specific number of includes on comments where the author gave an obvious caveat. Also that comment was from 2005 and there have been some improvements in the performance of includes since then (which was the basis of Schlossnagle's concern about includes).
I also think that Schlossnagle was hinting at some psychology here. I also use the 5-10 includes rule -- but my applications actually use around 15. That's not a failure on my part. The rule keeps me pushing to remove unnecessary includes. But I am not so foolish to hold the line on numbers that are "made up."
Posted: Thu Aug 09, 2007 4:21 pm
by timvw
As others already posted: It was not my intention to write someone off...
What it (imho) really comes down to is the following: Include as much as you have to... (not more, and certainly not less)
I'd rather have 5 includes, because all my scripts are rather small, and only contain what is absolutely necessary than one giant script that contains 500k with functions that i won't use anyway... (Btw, once you have an opcode cache, tip#1 in the article i referred to, you don't need the parsing, and thus the including, anymore

)
If after a while i have performance problems and testing points out that the largest improvements can be gained by reducing the number of includes, i won't doubt a second to deploy single file that contains all the code in the production environment. (I think i'd still keep the separate files for development though)