Page 1 of 1

Best practices with Includes

Posted: Fri Jul 22, 2005 11:33 am
by klopp
We are developing a large application in which we have done a good job of seperating our logic into numerous class files. However as such now when putting our application together we are finding that we have a large number of includes within our code (generally at the top of our files). Now to my knowledge includes are notoriously slow, and we often are using include_once's which are from my understanding even slower. So I think having such a large number of includes in our code is causing it to run a bit slow (after using apd we noticed it is consuming the majority of our run time).

So finally to the question, is there any best practice in PHP for handling a large number of includes? Ways to speed it up, apart from condensing all our class files into one?

Posted: Fri Jul 22, 2005 11:49 am
by nielsene
Have you benchmarked the code to see if the includes are really contributing to the speed of the site?

In my experience includes seldom dominate the "setup" cost of s script; most large apps I've seen do significant setup work -- establishing databasse connection(s), prepopulatting certain data structures, etc. In most cases this setup work is relagating to the include files.

The act of including isn't what's slow, but just the sheer amount of work being done there.

That said, one partial solution, is to be selective about your includes. The site I work on the most has several different functional areas, with almost disjoint class needs -- therefore I have seperate "header" files for each that include just what they need.

Each user(or admin)-viewable web-page require_once's "include_others" it does some logging,authentication is needed,
and then parses the URL to decide which batch of other includes to run. In cases where a few lone scripts need extra classes than the "stock" batch, they'll require_once those directly after the "call" to include_others.

Its kept performence acceptable -- at least as things are added to one functional area it doesn't degrade the others.

Re: Best practices with Includes

Posted: Fri Jul 22, 2005 12:46 pm
by Roja
klopp wrote:Now to my knowledge includes are notoriously slow, and we often are using include_once's which are from my understanding even slower.
Actually, the difference in speed between includes and include_once's is down to a negligible amount now. It was a problem in the early php-4 days, but not anymore. Include_once for the win. :)
klopp wrote: So I think having such a large number of includes in our code is causing it to run a bit slow (after using apd we noticed it is consuming the majority of our run time).
Well, first and foremost, if you care about speed *at all*, you should be using some form of accelerator. I can highly recommend e-accelerator, which is based off the (now discontinued) turck mmcache. Similarly, my host runs Zend accelerator, which is also excellent (although not free like e-a).

With an accelerator, the first hit has impact, but all additional hits are just a pull from memory (as long as the page hasnt changed, etc). It reduces load a HUGE amount.
klopp wrote: So finally to the question, is there any best practice in PHP for handling a large number of includes? Ways to speed it up, apart from condensing all our class files into one?
Make them selective and dynamic. Specifically, don't include ("Global_classes") into every file in your app. If page2 only needs 3 functions, include those, not the entire 7,000 class collection in one file.

Include speed is really not that big of a hit, and if it is, you should look at the harddrive settings on the machine you are using.

I have a file that includes over a dozen files, and microtime shows it to have used less than a tenth of a second.. hardly a huge impact, when the processing time on average is over half a second.