in reply to Very basic question on performance

Your examples do not produce the same results. The first one multiplies $x_sq * $y_sq, while the second example adds the equivalent terms. In my benchmark I changed the second code snippet to multiply instead.

The Benchmark module is sort of the standard module used in testing and comparing code snippets. The following script is an example of how to use it to check what your intuition is telling you:

use Inline C => 'DATA'; use Benchmark 'cmpthese'; sub vdiag { my( $x, $y ) = @_; my $x_sq = $x * $x; my $y_sq = $y * $y; my $prod = $x_sq * $y_sq; my $res = sqrt($prod); return $res; } sub ediag { return sqrt( $_[0] * $_[0] * $_[1] * $_[1] ); } cmpthese( -5, { cdiag => 'cdiag(7,9)', vdiag => 'vdiag(7,9)', ediag => 'ediag(7,9)', } ); __DATA__ __C__ #include <math.h> double cdiag ( double x, double y ) { double x_sq = x * x; double y_sq = y * y; double prod = x_sq * y_sq; return sqrt(prod); }

That produces the following comparison table:

$ ./mytest.pl Rate vdiag ediag cdiag vdiag 1589859/s -- -49% -82% ediag 3127854/s 97% -- -64% cdiag 8603134/s 441% 175% --

So your intuition was correct, that declaring a bunch of variables takes a little longer. But you do have to put it in perspective; 1589859 iterations per second is already very fast. And if you are really concerned with performance in a tight loop or something, eliminating the clarity that comes from using named variables is, in this case, not the most effective solution. The C solution is still readable, and achieves 8603134 iterations per second. Write your Perl how you would like to read it back in six months. And when performance is a real bottleneck, take your biggest swing at the problem first.


Dave

Replies are listed 'Best First'.
Re^2: Very basic question on performance
by AnomalousMonk (Archbishop) on Mar 14, 2014 at 13:58 UTC
    Write your Perl how you would like to read it back in six months.

    Write your Perl how you would like to read it back in six months at 3 AM, and your deadline to deliver your working, tested project is 8 AM.

      Or in my case, at 9:30am, with the 3yo and 5yo kids, under my "supervision", playing nearby. ;)


      Dave

        Ah, the joys of self employment! Or are you off-site/working-from-home, but otherwise normally employed? Not that it's any of my business...

Re^2: Very basic question on performance
by Laurent_R (Canon) on Mar 14, 2014 at 09:37 UTC
    I often use named variable for better clarity, but in this case, I find ediag to be more readable than vdiag, because you see immediately the math formula. It is by looking at the ediag function that I quickly saw that it can be simplified to:
    sub ediag { return $_[0] * $_[1]; }
    as I said in my other post. I would not pick up that immediately when looking at vdiag.

      Or more accurately:

      sub ediag { return abs($_[0]) * abs($_[1]); }

      ...or...

      sub ediag { abs(shift) * abs(shift); }

      It's possible (maybe even probable) that since the OP showed one subroutine that did this:

      sub one { sqrt( $_[0] * $_[0] * $_[1] * $_[1] ) }

      ...and another that did this:

      sub another{ sqrt( $_[0] * $_[0] + $_[1] * $_[1] ) }

      ...that we (I) should have been looking at his second example all along, and should have considered the first one a typo. If that's the case, then the square root of the sum of two squares does make more sense.


      Dave

        Thanks Dave, you were correct. I had a typo. It was suppose to be the square root of the sum of the squares for both. :-)
        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.
Re^2: Very basic question on performance
by Old_Gray_Bear (Bishop) on Mar 15, 2014 at 23:30 UTC
    Write your Perl like it's 0300, the ENTIRE FINANCIAL SYSTEM is down, and the Union Payroll has to be delivered by 0800. Oh, and you have three levels of Management on a conference call asking you "How's it going?" every five minutes. AND the coffee pot just burned out the heating element.

    ----
    I Go Back to Sleep, Now.

    OGB