Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: value accumulation

by Bilbo (Pilgrim)
on May 15, 2003 at 11:14 UTC ( [id://258357]=note: print w/replies, xml ) Need Help??


in reply to value accumulation

Do you just mean that you want to clear @goodnums before processing each line? If so then you probably just want to declare the array within the loop so that it is reinitialised each time

foreach my $line (@file) { my @good_nums; ... ... }

Replies are listed 'Best First'.
Re: Re: value accumulation
by Jenda (Abbot) on May 15, 2003 at 19:21 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://258357]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-24 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found