> > > I'm a litttle puzzled as to why max2 (foreach with if modifier) is > > > consistently about 25% faster than max4 (foreach with ternary operator). > > > My guess is that the difference is due to how often the assignment is > > > done. With the if modifier, the assignment is done only when necessary; > > > with the ternary operator, the assignment is done for every element of > > > the array (most of the time uselessly assigning $max = $max). > > > > > > > Useless assignment in Perl is costly, like any language. Perl also has to > > do reference counting that means it takes a few extra cycles. A more fair > > experiment is to benchmark if/else vs. ternary operator where each condition > > does the same thing... they should match, otherwise somebody hasn't optimised > > it correctly. > > Speaking of optimisation: Shouldn't this useless assignment be optimised > away? I can't think of a situation where assigning a variable to itself would > do anything but consume CPU time, so it seems from my (unqualified?) point of > view that this would be a good target for optimisation...
Yes, it should be optimised away, but why would you do it in the first place? If you tell it to do something, then why should it shy away from doing what it was told? Optimising things that don't occur in everyday programming is a waste of time, you can only do so much. Since the optimiser runs when you start the program... there is a tradeoff between waiting ten minutes for a fully optimised piece of code, or you can put up with a little slowness and your simple script returns in a matter of seconds. Read the chapter on compiling, it has some details on the steps perl goes through. The architecture of the compiler/optimiser is important too, Perl 5 allows tied variables... so $test = $test might not be as simple as it looks. An assignment here could increment the value of $test, phone for a pizza or instruct your bank to donate money from your account to needy causes. The optimiser would have to decide what is actually going on, and take the appropriate action. I'm not an internals person, so I can't say exactly what's going on in Perls innards. However, I hope to stimulate at least some conversation/thought. Anyway, take care, Jonathan Paton __________________________________________________ Do You Yahoo!? Everything you'll ever need on one web page from News and Sport to Email and Music Charts http://uk.my.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]