Note that PHP arrays are fast
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
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
On 4/9/07, Rob Thorne <rob@torenware.com> wrote:
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.
I actually thought the second was more readable... Is there a consensus on the readability? Greg
$flip = array('even' => 'odd', 'odd' => 'even'); $class = $flip[$class]; I think the second is more readable too and allows us to easily to chains if we want tri-color rows or rainbows. :) $flip = array('light' => 'dark', 'dark' => 'medium', 'medium' => 'light'); $class = $flip[$class]; Rob Roy Barreca Founder and COO Electronic Insight Corporation http://www.electronicinsight.com rob@electronicinsight.com
Greg Knaddison - GVS wrote:
On 4/9/07, Rob Thorne <rob@torenware.com> wrote:
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.
I actually thought the second was more readable...
Is there a consensus on the readability?
Greg
On 10 Apr 2007, at 04:35, Rob Barreca wrote:
$flip = array('even' => 'odd', 'odd' => 'even'); $class = $flip[$class]; I think the second is more readable too and allows us to easily to chains if we want tri-color rows or rainbows. :)
*lol* :) -- Dries Buytaert :: http://www.buytaert.net/
On 4/10/07, Rob Thorne <rob@torenware.com> wrote:
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.
Agreed.
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.
Huh, that's interesting. As someone who spent years maintaining a huge multi-process application written in assembly language -- where jump tables (dispatch arrays, essentially) are The Rule -- I find case statements much easier to read in most (not all) situations. I think that means we may not reach a consensus on readability for some such things.
On Tuesday, 10. April 2007, 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];
Just out of interest, what's with if ($class !== 'even') { $class = 'odd'; } ? Wouldn't this avoid half the assignments and replace the other half with a simple check (while making the code still a bit more readable)? Maybe there's some optimization magic that I don't know of, though. Regards, Jakob
Its slightly off topic, buw while we are at it, I benchmarked if ($foo) {bar()} versus $foo ? bar () : null; Op dinsdag 10 april 2007 08:20, schreef Jakob Petsovits:
Just out of interest, what's with
if ($class !== 'even') { $class = 'odd'; }
if ($foo) { $bar } is about 7% faster then $foo ? $bar : null; The reason is, obviously, that the second always needs an else. And if() else statement is just as fast as the $foo? : ; way. We should avoid $foo ? checks that need no else at all costs, imo. Bèr
On 10.04.2007, at 08:20, Jakob Petsovits wrote:
if ($class !== 'even') { $class = 'odd'; }
I fail to see how this would osolve the problem. Konstantin Käfer – http://kkaefer.com/
On Tuesday, 10. April 2007, Konstantin Käfer wrote:
On 10.04.2007, at 08:20, Jakob Petsovits wrote:
if ($class !== 'even') { $class = 'odd'; }
I fail to see how this would osolve the problem.
Er. Where's that paper bag again.
My two cents are added to the closed ticket http://drupal.org/node/134697 Basically: * modulo is slower than bitwise-and and I think that this was the main culprit to the even/odd flip performance * microtimer is not a benchmarking tool Doug Green 904-583-3342 www.douggreenconsulting.com Bringing Ideas to Life with Software Artistry and Invention... Providing open source software political solutions -----Original Message----- From: development-bounces@drupal.org [mailto:development-bounces@drupal.org] On Behalf Of Karoly Negyesi Sent: Monday, April 09, 2007 8:19 PM To: development@drupal.org Subject: [development] Note that PHP arrays are fast 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
* modulo is slower than bitwise-and and I think that this was the main culprit to the even/odd flip performance
But the patch was a bugfix and the race was between $class = $class == 'even' ? 'odd' : 'even' $class = $flip[$class]; Because the key is not reliable, so we must flip the class.
* microtimer is not a benchmarking tool
I know that the microtime is not accurate but -- what else if you need subsecond? Even the most ancient PCs had a 1/18.2 second timer, I am not intimate on newer designs. Regards, NK
On 11 Apr 2007, at 03:55, Karoly Negyesi wrote:
* modulo is slower than bitwise-and and I think that this was the main culprit to the even/odd flip performance
But the patch was a bugfix and the race was between
$class = $class == 'even' ? 'odd' : 'even'
$class = $flip[$class];
Because the key is not reliable, so we must flip the class.
Yes, in that particular patch, the if-test would sometimes break (depending on the input data). The patch fixed a bug, and the tiny performance improvement was just a secondary effect. -- Dries Buytaert :: http://www.buytaert.net/
participants (10)
-
Bèr Kessels -
Chris Johnson -
Doug Green -
Dries Buytaert -
Greg Knaddison - GVS -
Jakob Petsovits -
Karoly Negyesi -
Konstantin Käfer -
Rob Barreca -
Rob Thorne