in reply to Re^3: Very basic question on performance
in thread Very basic question on performance

Yes, Dave, you are correct, it should rather be something like:
return abs ( $_[0] * $_[1]);
But my point was really to say that, although I am very often using named variables for better clarity (and also to avoid changing inadvertently the parameter in the calling function), I found that, in this case, the ediag function is in my humble opinion actually clearer than the vdiag function, because you can see immediately the math expression being returned to the caller.

Replies are listed 'Best First'.
Re^5: Very basic question on performance
by davido (Cardinal) on Mar 14, 2014 at 18:57 UTC

    Yes, I'll agree. With that in mind, and given another followup from the OP, it seems that what he really wants is this:

    sub diag { sqrt( $_[0]^^2 + $_[1]^^2 ); }

    ...or possibly...

    sub diag { sqrt( shift^^2 + shift^^2 ); # Not sure if this is more readable, or + not. }

    Or if he's in a really tight loop where every cycle counts, possibly an Inline::C version of the same.


    Dave

      Yes, I agree and had noted the OP's new message. In fact, I would consider this:
      sub diag { my ($x, $y) = @_; sqrt( $x^^2 + $y^^2 ); }
      to still be very fine (perhaps slightly slower, but very clear), but the way the original script was coded with numerous intermediate variables (to store the squares, etc.) actually made the things more complicated to follow than a single and simple math expression which all of us presumably can understand at first glance.