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


In reply to Re: Very basic question on performance by davido
in thread Very basic question on performance by morrin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.