Re: PHP 5.1 vm

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

 



On Wed, 2005-07-06 at 17:51, Richard Lynch wrote:
> On Tue, July 5, 2005 10:55 pm, Dan Rossi said:
> >
> > On 06/07/2005, at 3:43 PM, Rasmus Lerdorf wrote:
> >
> >> The SWITCH vm was originally just a big switch(opcode) { case 1: ...;
> >> case 2: ...' } It's a bit different now, but you can think of it in
> >> those terms.  Decent compilers should theoretically be able to optimize
> >> a clean set of cases to something close to simple branches, just like
> >> the computed goto case, but it can be hit and miss.  With the computed
> >> goto way of doing it, we are trying to send a stronger hint to the
> >> compiler.
> >
> > Well i do find a performance issue running a heap of switches in PHP so
> > I could presume the same here.
> 
> I believe that what's supposed to happen, in theory, is that the compiler
> *CONVERTS* the swith statement to a bunch of GOTOs behind the scenes.
> 
> At least I think that's what Rasmus means by "simple branches" -- GOTOs.
> 
> When this works, you get the speed of GOTOs with the syntactic sugar of
> SWITCH for us humans to look at. :-)

Switches are as fast as their counter part the if/elseif/else blocks.
Run time is O(n) given that each switch branch is equally likely to run
for the data set. If you know that a specific condition will occur 99.9%
of the time, you can optimize by making it the first case in your switch
statement (as you could also make it your first "if" check in the
if/elseif/else scenario). If you have a very large number of options
then you may be able to optimize using an array to hold function
pointers, here the issue is whether the O(lg n) lookup of the function
pointer in the array offsets the overhead incurred for invoking a
function. You can't make it O(1) by using GOTOs unless you have constant
data that would allow the compiler to prepared constant jumps
beforehand.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
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