Re: [PHP] efficiency of include()

2009-12-20 Thread Nathan Rixham
Daniel Kolbo wrote:
> Larry Garfield wrote:
>> On Sunday 20 December 2009 10:45:45 am Daniel Kolbo wrote:
>>> Hello PHPers,
>>>
>>> This is a two part question:
>>>
>>> 1) Is it faster to include one file with lots of code, or many separate
>>> smaller individual files?  Assume the one massive file is merely the
>>> concatenation of all the smaller individual files.  (I am assuming the
>>> one massive file would be faster..., but i wanted to get confirmation).
>> Conventional wisdom is that the one big file is faster, since it requires 
>> one 
>> disk I/O hit instead of several.  HOWEVER, if you're only using a small 
>> portion of that code then it could be faster to load only the code you 
>> really 
>> need.  Where the trade off is varies with your architecture, the amount of 
>> code, ad how good the disk caching of your OS is.
>>
>>> 2) Suppose php has to invoke the include function 100 times.  Suppose
>>> all files are on average the same size and contain the same number of
>>> instructions.  Would it be faster to include the same exact file 100
>>> times as opposed to 100 different file names?  Basically, does the
>>> engine/parser take any shortcuts if it notices that the file name has
>>> already been included once?
>> I'm pretty sure that PHP will recognize that it's already parsed that file 
>> and 
>> keep the opcode caches in memory, so it needn't hit disk again.  I've not 
>> checked into that part of the engine, though, so I may be wrong there.
>>
> 
> Thanks for the reply.
> 
> For 2): I've often searched for php parsing documentation.  I love the
> php.net documentation.  However, i have yet to find an excellent source
> documenting the php parser/engine.  My searches always yield the zend
> website, but it doesn't seem like i can get very far from that page.
> Any suggestions on where i could learn more of the nitty gritty details
> of the php/zend behaviours?
> 
> Thanks,
> dK
> `

Daniel,

I'm only replying because I've been down this route of taking everything
in to consideration countless times now.

Code optimisation, sql and db optimisation, database and web server
tuning all have a huge impact in comparison to the things your
considering, and as such should probably be given more weight.

Further, the next obvious steps are to get zend optimizer (which
optimizes the opcodes) then a good opcode cache; and finally cache all
the output you can so that php doesn't even have to come in to the
equation for most "hits".

Then, finally you get down to the bits you're considering for those
extra microseconds and the knowledge that you've done good; whether
it'll make any difference at this point or not is another issue :p

bit of light reading for you:
http://phplens.com/lens/php-book/optimizing-debugging-php.php

regards!

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



Re: [PHP] efficiency of include()

2009-12-20 Thread Daniel Kolbo
Larry Garfield wrote:
> On Sunday 20 December 2009 10:45:45 am Daniel Kolbo wrote:
>> Hello PHPers,
>>
>> This is a two part question:
>>
>> 1) Is it faster to include one file with lots of code, or many separate
>> smaller individual files?  Assume the one massive file is merely the
>> concatenation of all the smaller individual files.  (I am assuming the
>> one massive file would be faster..., but i wanted to get confirmation).
> 
> Conventional wisdom is that the one big file is faster, since it requires one 
> disk I/O hit instead of several.  HOWEVER, if you're only using a small 
> portion of that code then it could be faster to load only the code you really 
> need.  Where the trade off is varies with your architecture, the amount of 
> code, ad how good the disk caching of your OS is.
> 
>> 2) Suppose php has to invoke the include function 100 times.  Suppose
>> all files are on average the same size and contain the same number of
>> instructions.  Would it be faster to include the same exact file 100
>> times as opposed to 100 different file names?  Basically, does the
>> engine/parser take any shortcuts if it notices that the file name has
>> already been included once?
> 
> I'm pretty sure that PHP will recognize that it's already parsed that file 
> and 
> keep the opcode caches in memory, so it needn't hit disk again.  I've not 
> checked into that part of the engine, though, so I may be wrong there.
> 

Thanks for the reply.

For 2): I've often searched for php parsing documentation.  I love the
php.net documentation.  However, i have yet to find an excellent source
documenting the php parser/engine.  My searches always yield the zend
website, but it doesn't seem like i can get very far from that page.
Any suggestions on where i could learn more of the nitty gritty details
of the php/zend behaviours?

Thanks,
dK
`

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



Re: [PHP] efficiency of include()

2009-12-20 Thread Larry Garfield
On Sunday 20 December 2009 10:45:45 am Daniel Kolbo wrote:
> Hello PHPers,
>
> This is a two part question:
>
> 1) Is it faster to include one file with lots of code, or many separate
> smaller individual files?  Assume the one massive file is merely the
> concatenation of all the smaller individual files.  (I am assuming the
> one massive file would be faster..., but i wanted to get confirmation).

Conventional wisdom is that the one big file is faster, since it requires one 
disk I/O hit instead of several.  HOWEVER, if you're only using a small 
portion of that code then it could be faster to load only the code you really 
need.  Where the trade off is varies with your architecture, the amount of 
code, ad how good the disk caching of your OS is.

> 2) Suppose php has to invoke the include function 100 times.  Suppose
> all files are on average the same size and contain the same number of
> instructions.  Would it be faster to include the same exact file 100
> times as opposed to 100 different file names?  Basically, does the
> engine/parser take any shortcuts if it notices that the file name has
> already been included once?

I'm pretty sure that PHP will recognize that it's already parsed that file and 
keep the opcode caches in memory, so it needn't hit disk again.  I've not 
checked into that part of the engine, though, so I may be wrong there.

-- 
Larry Garfield
la...@garfieldtech.com

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



[PHP] efficiency of include()

2009-12-20 Thread Daniel Kolbo
Hello PHPers,

This is a two part question:

1) Is it faster to include one file with lots of code, or many separate
smaller individual files?  Assume the one massive file is merely the
concatenation of all the smaller individual files.  (I am assuming the
one massive file would be faster..., but i wanted to get confirmation).

2) Suppose php has to invoke the include function 100 times.  Suppose
all files are on average the same size and contain the same number of
instructions.  Would it be faster to include the same exact file 100
times as opposed to 100 different file names?  Basically, does the
engine/parser take any shortcuts if it notices that the file name has
already been included once?

I would test this, but i don't want to create hundreds of different files...

Thanks,
dK
`

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



Re: [PHP] Efficiency question

2006-08-04 Thread Richard Lynch
On Tue, July 25, 2006 11:41 pm, Paul Scott wrote:
> I have googled around a bit, but not really found anything useful...
>
> Which is more efficient? A case switch or a $$method style method?
>
> An example:
>
> switch($action) {
> case 'edit':
>   //do some stuff
>   ...
>   return "edit_tpl.php";
>
> case 'whatever':
>   //blah
>   ..
>   ..
> }
>
> OR:
>
> $method=$this->convertToMethod($action);

A user-defined function will almost always be more expensive than a
built-in language construct...

If you were willing to just do:

$this->$action();

and to hell with validating $action to be sure it was kosher, it would
be faster...

But your validation of $action to be sure it's not an internal method
you don't want them calling would probably look something like:
switch($action){
  case 'known_action':
  case 'other_action':
return true;
  break;
  default:
error_log("Possible hack attempt $_SERVER[REMOTE_ADDR]");
echo "Invalid Action";
return false;
  break;
}

So now you have a switch/case just as big as you would have had anyway.

> unset($action);
> return $this->$method();
>
> Hope this is reasonably clear... Note the return on the latter code.
>
> If anyone has any ideas around this, please let me know! Are there
> underlying security risks in doing it this way?

If convertToMethod() is not checking against a known list of valid
'action' you are making a security hole.

-- 
Like Music?
http://l-i-e.com/artists.htm

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



Re: [PHP] Efficiency question

2006-07-26 Thread Jochem Maas
Paul Scott wrote:
> I have googled around a bit, but not really found anything useful...
> 
> Which is more efficient? A case switch or a $$method style method?
> 

both examples are just boilerplate 'frontcontroller' code that do pretty much 
nothing
of themselves.

- in terms of speed you will notice nothing. in principle OO code is a little 
slower
because creating objects has quite a lot of overhead as compared to just calling
functions (if your just creating 1 or 2 objects it's not measurable) - but then
it all depends on what the code inside object/function actually does!

> An example:
> 
> switch($action) {
> case 'edit':
>   //do some stuff
>   ...
>   return "edit_tpl.php";
> 
> case 'whatever':
>   //blah
>   ..
>   ..
> }
> 
> OR:
> 
> $method=$this->convertToMethod($action);

my guess is that there will be a switch statement in convertToMethod()
to 'convert' $action into $method. so it effectively does what you example 
switch()
is doing.

ask yourself whether you prefer to hide some of the complexity of what your
scripts are doing inside the bowels of an object - or whether you prefer to
use procedural code. either is fine, it's up to you - just make sure you write 
neat,
well-documented & properly thoughtout code and even the guy that has to 
maintain your
stuff 2 years from now will be happy regardless of the paradigm you chose.

> unset($action);
> return $this->$method();
> 
> Hope this is reasonably clear... Note the return on the latter code.
> 
> If anyone has any ideas around this, please let me know! Are there
> underlying security risks in doing it this way?

$action is coming from the client (eg. a GET/POST variable) - sanitize it!
validate it! use 'whitelisting' to trigger a reaction (rather than using the
actual value). don't trust data coming from outside your script/server (don't 
trust
that either ;-).


> 
> --Paul
> 
> 
> 
> 
> 
> All Email originating from UWC is covered by disclaimer  
> http://www.uwc.ac.za/portal/uwc2006/content/mail_disclaimer/index.htm 
> 
> 

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



Re: [PHP] Efficiency question

2006-07-26 Thread Rory Browne

For something like that that, unless you are doing it inside a loop, I
wouldn't really worry about efficiency as much as I would about security

On 7/26/06, Paul Scott <[EMAIL PROTECTED]> wrote:



I have googled around a bit, but not really found anything useful...

Which is more efficient? A case switch or a $$method style method?

An example:

switch($action) {
case 'edit':
  //do some stuff
  ...
  return "edit_tpl.php";

case 'whatever':
  //blah
  ..
  ..
}

OR:

$method=$this->convertToMethod($action);
unset($action);
return $this->$method();

Hope this is reasonably clear... Note the return on the latter code.

If anyone has any ideas around this, please let me know! Are there
underlying security risks in doing it this way?

--Paul



All Email originating from UWC is covered by disclaimer
http://www.uwc.ac.za/portal/uwc2006/content/mail_disclaimer/index.htm



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




[PHP] Efficiency question

2006-07-25 Thread Paul Scott

I have googled around a bit, but not really found anything useful...

Which is more efficient? A case switch or a $$method style method?

An example:

switch($action) {
case 'edit':
  //do some stuff
  ...
  return "edit_tpl.php";

case 'whatever':
  //blah
  ..
  ..
}

OR:

$method=$this->convertToMethod($action);
unset($action);
return $this->$method();

Hope this is reasonably clear... Note the return on the latter code.

If anyone has any ideas around this, please let me know! Are there
underlying security risks in doing it this way?

--Paul

All Email originating from UWC is covered by disclaimer  
http://www.uwc.ac.za/portal/uwc2006/content/mail_disclaimer/index.htm 

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

[PHP] RE: use of mysql_free_result (was Re: [PHP] Efficiency)

2002-09-19 Thread Tim Ward

if your query is within a loop then it would probably help, e.g.

for ($i = 1; $i <= 1000; $i++)
{   $result = mysql_query("...");
...
}

in this case as $result is a resource identifier then reusing
it doesn't release the original result.

Tim Ward
www.chessish.com


> -Original Message-
> From: Support @ Fourthrealm.com [mailto:[EMAIL PROTECTED]]
> Sent: 19 September 2002 01:18
> To: [EMAIL PROTECTED]
> Subject: use of mysql_free_result (was Re: [PHP] Efficiency)
> 
> 
> Rick, or anyone,
> 
> Based on what you said below, can you describe for me when the 
> mysql_free_result tag should be used, and when it is not necessary?
> 
> I'm fluent in other web languages (iHTML, ASP), but am fairly 
> new to PHP, 
> so I'm still learning the intricacies of the language, and 
> the best way to 
> use it
> 
> Many thanks,
> Peter
> 
> 
> 
> At 04:02 PM 9/18/2002 -0600, you wrote:
> >If you aren't doing anything else in a script, 
> mysql_free_result is not needed
> >in a script like this because the result set will be cleaned 
> up by PHP when
> >the script ends.
> 
> - - - - - - - - - - - - - - - - - - - - -
> Fourth Realm Solutions
> [EMAIL PROTECTED]
> http://www.fourthrealm.com
> Tel: 519-739-1652
> - - - - - - - - - - - - - - - - - - - - -
> 
> 

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




Re: [PHP] use of mysql_free_result (was Re: [PHP] Efficiency)

2002-09-18 Thread Chris Shiflett

This frees the memory that is used to store the results of your query 
(the $result variable in most examples).

Many people leave this out, because PHP does a lot of cleanup for you 
when your script exits. However, it is a good habit to go ahead and free 
your results once you are finished using them. It is never really 
necessary, but imagine if you have an enormous result set that takes a 
great deal of memory, you have many people hitting your site, and you 
have a great deal of logic remaining before your script executes. The 
amount of wasted memory can be significant in some cases, so you can use 
this function to keep the "damage" low.

Happy hacking.

Chris

Support @ Fourthrealm.com wrote:

> Based on what you said below, can you describe for me when the 
> mysql_free_result tag should be used, and when it is not necessary? 



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




[PHP] use of mysql_free_result (was Re: [PHP] Efficiency)

2002-09-18 Thread Support @ Fourthrealm.com

Rick, or anyone,

Based on what you said below, can you describe for me when the 
mysql_free_result tag should be used, and when it is not necessary?

I'm fluent in other web languages (iHTML, ASP), but am fairly new to PHP, 
so I'm still learning the intricacies of the language, and the best way to 
use it

Many thanks,
Peter



At 04:02 PM 9/18/2002 -0600, you wrote:
>If you aren't doing anything else in a script, mysql_free_result is not needed
>in a script like this because the result set will be cleaned up by PHP when
>the script ends.

- - - - - - - - - - - - - - - - - - - - -
Fourth Realm Solutions
[EMAIL PROTECTED]
http://www.fourthrealm.com
Tel: 519-739-1652
- - - - - - - - - - - - - - - - - - - - -


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




Re: [PHP] Efficiency

2002-09-18 Thread Rick Widmer

At 04:41 PM 9/18/02 -0400, Support @ Fourthrealm.com wrote:
>Which is more efficient?

Considering that the difference in efficiency is so small, a more important
question is which is clearer to the programmer?

I prefer b, except that I allow short tags and use the magic print/echo 
function:

  thusly...


$result = mysql_query( ... );

?>


Title





   








PHP by itself is a fantastic template language!

If you aren't doing anything else in a script, mysql_free_result is not needed
in a script like this because the result set will be cleaned up by PHP when
the script ends.

Rick



>a) a sql loop where everything is displayed/formatted using echo stmts, 
>like this:
>
>$result = mysql_query("SELECT * FROM news WHERE active=1");
>while ($row = mysql_fetch_object($result)) {
> echo "$row->title ";
>}
>mysql_free_result($result);
>
>?>
>
>
>OR
>b) a sql loop where you break in and out of php tags as needed:
>
>$result = mysql_query("SELECT * FROM news WHERE active=1");
>while ($row = mysql_fetch_object($result)) {
>?>
>
> title"; ?>
>
>
>}
>mysql_free_result($result);
>
>?>
>
>
>Obviously, these are really simplified examples.  Typically the html code 
>gets much more complicated.
>
>
>Peter
>
>
>- - - - - - - - - - - - - - - - - - - - -
>Fourth Realm Solutions
>[EMAIL PROTECTED]
>http://www.fourthrealm.com
>Tel: 519-739-1652
>- - - - - - - - - - - - - - - - - - - - -
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
>


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




[PHP] Efficiency

2002-09-18 Thread Support @ Fourthrealm.com

Which is more efficient?

a) a sql loop where everything is displayed/formatted using echo stmts, 
like this:

$result = mysql_query("SELECT * FROM news WHERE active=1");
while ($row = mysql_fetch_object($result)) {
 echo "$row->title ";
}
mysql_free_result($result);

?>


OR
b) a sql loop where you break in and out of php tags as needed:



 title"; ?>





Obviously, these are really simplified examples.  Typically the html code 
gets much more complicated.


Peter


- - - - - - - - - - - - - - - - - - - - -
Fourth Realm Solutions
[EMAIL PROTECTED]
http://www.fourthrealm.com
Tel: 519-739-1652
- - - - - - - - - - - - - - - - - - - - -


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