Page 1 of 1

Grouping Included Functions-How much is too much? [RESOLVED]

Posted: Wed Sep 20, 2006 8:12 am
by opengavel
I was just wondering if anyone has any guidelines or rules-of-thumb on how many included functions is too many?

I have about 10 or 12 functions that I use across say 30 pages. Is it better to group them all in one include file, break them up into say 3 or 4 different include files (if possible), or nest them (grouping them by what they do -- i.e., file manip functions, DB functions, etc.) I know this doesn't seem like a lot of includes, but I expect the number to grow substantially and I would like to settle on a general approach before it grows much more.

It would seem that nesting them would be the most orderly way, but would that affect the speed or performance of the scripts?

In other words, is the script slowed down by the functions it does not use or by too many file inclusions?

Thanks,

(P.S., Although this isn't a code question, I wasn't sure it should go in Theory and Design either since the question is not "advanced").

Posted: Wed Sep 20, 2006 8:30 am
by jayshields
I would group them by what they do, and include them all. This way the code would be most readable.

I think

Code: Select all

include('functions/db.php');
include('functions/manip.php');
is better than

Code: Select all

include('functions/one.php');
include('functions/two.php');
include('functions/three.php');
include('functions/four.php');
or

Code: Select all

include('functions.php');

Posted: Wed Sep 20, 2006 3:43 pm
by jwalsh
Files are cheap. Organization is key to portable code.

Posted: Thu Sep 21, 2006 10:48 am
by opengavel
Thanks for the replies.

I am still wondering a couple of things:

1. Is a PHP script slowed down by unused functions?

2. Even if I group them by topic and include them, do others generally include all the functions or just the groups that include functions you know you will use? I assume the answer to this depends on the first question.

3. Lastly, does anyone have a simple script that can output a script's execution time, so I can measure the speed difference, if any?

Thanks again

Posted: Thu Sep 21, 2006 10:56 am
by jwalsh
Unless you are including massive (and I mean MASSIVE) amounts of unused functions, you won't notice a difference.

Calculating execution time is easy

Top of script:

Code: Select all

$begin = microtime();
End of Script:

Code: Select all

$end = microtime();
$elapsed = $end - $begin;
echo "Execution Time: " . $elapsed . " milliseconds";

Posted: Thu Sep 21, 2006 11:01 am
by opengavel
Thanks

Posted: Thu Sep 21, 2006 11:02 am
by alex.barylski
PHP in terms of execution wouldn't be slowed by unused functions, but parsing time is increased and therefore does effect your overall PHP performance. Reduce the amount of unused functions as much as possible.

Modularity is key to PHP development success.

Files under ext2 FS are actually accessed faster when they fall under the 12*1024 bytes rule. Meaning large files are accessed slower than small files (1024*12 = 12228 bytes OR LESS) so modularity, although requiring more includes and thus more wasted disk space *can* actually be faster in terms of overall execution.