On Nov 11, 2007 9:32 PM, Robert Cummings <robert@xxxxxxxxxxxxx> wrote: > On Mon, 2007-11-12 at 11:27 +1100, Chris wrote: > > Frank Lopes wrote: > > > I just started using PHP and got to think... > > > > > > Without getting into the discussion of "best practices", strictly from > > > a performance perspective, > > > what is faster: a function or an include? > > > > > > For example I have a block of text that needs to appear mutliple times > > > throughout the site. > > > > > > Will I be better off creating a function with its contents and then > > > later just calling the function or, > > > will it be faster (from an execution perspective) for me to create an > > > .inc file that gets included later on? > > > > Micro-optimization is pretty useless. I seriously doubt you would notice > > any difference in performance. > > > > This comes under the other discussions like about which is faster - a > > foreach/while/for loop. > > > > You'll find other bottlenecks (eg changing a regex to do an str_replace) > > which will make a bigger difference. > > Actually, if you're going to use a comparison between using regex and > string replacement as an example of lower hanging optimization fruit, I > think you'll find the difference between invoking a function and > including a file to be on par. The function is by far the better > solution when considering speed. > rob is right. i said screw it and wrote out a series of test scripts; just got curious. so, there is one file which includes an external script that operates on a variable in global scope; then there is a script which defines a function internally; then there is a script which includes another script with that function defined in it. the last one won each time; quite to my surprise. nathan@devel ~/working/www/phpExperiments/functionVsInclude $ php mainWInclude.php 10 totalTime: 0.001057 nathan@devel ~/working/www/phpExperiments/functionVsInclude $ php mainWFunction.php 10 totalTime: 0.000642 nathan@devel ~/working/www/phpExperiments/functionVsInclude $ php mainWExternalFunction.php 10 totalTime: 0.000604 ill show the code if anyone wants it. -nathan