Re: PHP 5.1 vm

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

 



On Wed, July 6, 2005 3:08 pm, Robert Cummings said:
>> > 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.

Which, in C, you HAVE only constant jumps.

You're not allowed to put variables in your "case:" statements in C. *

So a *GOOD* C pre-compiler, in theory, is going to re-write your switch()
to a bunch of GOTOs, before it hands it off to the main compiler to
actually turn it into Assembler.

That is what, I still believe, is the optimization Rasmus was talking about.

The C pre-compiler probably only does this optimization if it "thinks"
it's going to be "worth it", for some definition of "think" and "worth it"
as defined by the compiler writers and their reluctance to attempt to
figure out what the hell their compiler did when it converted that C code
into Assembler.

* I believe the main reason for only constants in C "case:" statements
*IS* so the pre-compiler can take your "slow" switch and re-factor it into
a bunch of GOTOs...  Or it could be just because the C guys are really
weird or something... :-)

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


[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