I'd go for the first version, with the temporary variable, for the exact reason you mention: the second version has an extra call to the subroutine. The extra memory overhead shouldn't be much of a problem. I don't think you're dealing with gigabites of data here at once.

Another reason the first version is preferable because the call to the subroutine in the if clause may have side effects that will affect the value that will be pushed onto the array. The following script demonstrates what I mean.

Update: there was a stupid little error in the script. Fixed.

use strict; use warnings; use subs qw( version1 version2 ); my @delete = qw(bar baz); my %hash = (foo => 1, bar => 2, baz => 3); print "Keys in the hash: ", join(", ", keys %hash), "\n"; print "Version 1 reports ", version1, " as the sum of values deleted f +rom the hash.\n"; print "Keys in the hash after version 1: ", join(", ", keys %hash), "\ +n"; print "\n"; %hash = (foo => 1, bar => 2, baz => 3); print "Keys in the hash: ", join(", ", keys %hash), "\n"; print "Version 2 reports ", version2, " as the sum of values deleted f +rom the hash.\n"; print "Keys in the hash after version 2: ", join(", ", keys %hash), "\ +n"; sub version1 { my $num_deleted = 0; for (@delete) { my $deleted = delete $hash{$_}; $num_deleted += $deleted if $deleted; } return $num_deleted } sub version2 { my $num_deleted = 0; for (@delete) { $num_deleted += delete $hash{$_} if delete $hash{$_} } return $num_deleted } __END__ Keys in the hash: bar, baz, foo Version 1 reports 5 as the sum of values deleted from the has. Keys in the hash after version 1: foo Keys in the hash: bar, baz, foo Use of uninitialized value in addition (+) at G:\y.pl line 35. Use of uninitialized value in addition (+) at G:\y.pl line 35. Version 2 reports 0 as the sum of values deleted from the hash. Keys in the hash after version 2: foo

In reply to Re: Temp variable performace vs Inline behavior by muba
in thread Temp variable performace vs Inline behavior by Ransom

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.