When I saw your post I thought "Yes, that's true. But it would be more efficient to declare the @good_nums array outside the look and just empty it". Luckily I did not go ahead and post that, but run a benchmark (In Perl 5.8, ActivePerl build 804, Win2kServer):

use Benchmark; use strict; sub arrayInit { foreach (1..1000) { my @list; foreach (1..10) { push @list, $_; } } } sub arrayUndef { my @list; foreach (1..1000) { foreach (1..10) { push @list, $_; } undef @list; } } sub arrayEmpty { my @list; foreach (1..1000) { foreach (1..10) { push @list, $_; } @list = (); } } sub scalarInit { foreach (1..1000) { my $sum; foreach (1..10) { $sum += $_; } } } sub scalarUndef { my $sum; foreach (1..1000) { foreach (1..10) { $sum += $_; } undef $sum; } } sub scalarEmpty { my $sum; foreach (1..1000) { foreach (1..10) { $sum += $_; } $sum = 0; } } timethese 1000, { arrayInit => \&arrayInit, arrayUndef => \&arrayUndef, arrayEmpty => \&arrayEmpty, scalarInit => \&scalarInit, scalarUndef => \&scalarUndef, scalarEmpty => \&scalarEmpty, } __END__ Benchmark: timing 1000 iterations of arrayEmpty, arrayInit, arrayUndef +, scalarEmpty, scalarInit, sca larUndef... arrayEmpty: 15 wallclock secs (13.97 usr + 0.00 sys = 13.97 CPU) @ 71 +.58/s (n=1000) arrayInit: 13 wallclock secs (13.45 usr + 0.00 sys = 13.45 CPU) @ 74 +.35/s (n=1000) arrayUndef: 16 wallclock secs (15.70 usr + 0.00 sys = 15.70 CPU) @ 63 +.68/s (n=1000) scalarEmpty: 9 wallclock secs ( 9.30 usr + 0.02 sys = 9.32 CPU) @ 1 +07.26/s (n=1000) scalarInit: 10 wallclock secs ( 9.36 usr + 0.01 sys = 9.37 CPU) @ 10 +6.68/s (n=1000) scalarUndef: 10 wallclock secs ( 9.43 usr + 0.00 sys = 9.43 CPU) @ 1 +06.01/s (n=1000)
So once again I was wrong :-|

Thanks for making me try.

Jenda
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
   -- Rick Osborne

Edit by castaway: Closed small tag in signature


In reply to Re: Re: value accumulation by Jenda
in thread value accumulation by Anonymous Monk

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.