[development] Note that PHP arrays are fast

Rob Thorne rob at torenware.com
Tue Apr 10 02:06:44 UTC 2007


While I use these kind of array tricks all the time (love that table 
driven code),  the advantage of the first form is readability.  Unless 
code like that is called many times in a request, I'd still go with the 
?: operator, since the intent is clearer.

Remember: we have to maintain this stuff after we write it.

On the other hand: I like dispatch arrays lots better than huge case 
statements.  I suspect that these are not only faster, but easier to 
understand as well.

Rob

Karoly Negyesi wrote:
> Hi,
>
> Note my trick in http://drupal.org/node/134697 which shows that the 
> hash lookups in Zend Engine are a bit faster than branching.
>
> Instead of
>
>   $class = $class == 'even' ? 'odd' : 'even'
>
> we have
>
>   $flip = array('even' => 'odd', 'odd' => 'even');
>   $class = $flip[$class];
>
> The first can be as much as 10% slower. I will explore this area 
> further and I recommend everyone to familiarize themselves with the 
> vast array of php array functions :) because next Drupal will be even 
> more abound with arrays.
>
> Regards,
>
> NK



More information about the development mailing list