RE: Structured Code vs. Performance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> I got different portions of code only used for certain purposes (who
> don't
> ;-)?). But what, in your opinion (better: in your experience) would be
> the
> best regarding script-performance: Putting each code-portion in a
> separate
> file and include it if required, putting it in a constant-dependent
> if-structure (if (defined('FOO') && FOO) {class foo{}; function foo();
> ...})
> or simply let it be parsed every time?
> 
> My first choice is using separate files, but if a file e.g. only
> contains 20
> lines, I fear it would take much longer to include the file against
> simply
> parsing these lines in the existing file. And as parsing is done really
> fast, there might be no real performance-loss in case of not using the
> mentioned code. With the constant-dependent if-structure I don't know
> if
> there are any performance-benefits if FOO isn't defined or defined as
> FALSE.
> 
> Looks for me a bit like a philosophical question, but maybe you have
> something to say about it nevertheless. A good thing for me would be
> something like: up to 125 lines of code you get an adequate performance
> with
> simply parsing it every time, with more than 125 lines you would get a
> better performance with using separate files - just kidding, surely the
> number of lines in this case is 42 ;-).
> 
> Looking forward to your answers
> Thomas

To my understanding (and anyone who thinks differently can correct me) there
is very little performance loss in including (or requiring) multiple files,
unless the number of files reaches something like over a hundred. This says
nothing about the amount of code in those file of course. But like you said,
parsing is fast.

I personally prefer the approuch:

1 directory = package
1 subdirectory = subpackage
1 .php file = class

I haven't noticed that much performance degradation in my scripts, while
have amounts several dozen of files.

You can avoid duplication by only using require_once or include_once. PHP
automatically does the checking that the file is not included if it has
already been included once. The Zend framework works this way for instance.
Just simply require_once, at the beginning of the file, everything the file
in question needs.

Constants on the global scope are a bit of a different case. But if you do a
lot of objects, you can instead of the global scope put all of your
constants into classes, which works just as well. This avoids name
conflicts.

Hope that answered your question.


Tomi Kaistila
PHP Developer

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux