in reply to using map and anonymous subroutines
A little note on efficiency.
First create a file (win32):
perl -wle"for ('00001'..'10000') { print join ' ', ($_) x 20; }" > + temp.tmp
Then get @temp in various ways and look at peak memory consumption.
open my $fh, 'temp.tmp' or die $!; # Alternative 1: 40 984 K my @temp = map { "$_\n" } map { split ' ' } <$fh>; # Alternative 2: 29 076 K my @temp = map { split ' ' } <$fh>; $_ .= "\n" for @temp; # Alternative 3: 14 816 K local $_; my @temp; push @temp => map "$_\n", split ' ' while <$fh>;
Alternative 2 is less memory hungry (as well as faster) because Perl doesn't have to build up an extra list. Alternative 3 is even less memory hungry because it doesn't slurp the file before building up @temp. Of course, there could've been a fourth alternativ having alternativ 2's for loop, but I think the point is made already.
(I use ActivePerl v5.8.0 built for MSWin32-x86-multi-thread, build 806.)
Hope this helps,
ihb
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: using map and anonymous subroutines
by flyingmoose (Priest) on Apr 20, 2004 at 22:04 UTC | |
|
Re: Re: using map and anonymous subroutines
by TomDLux (Vicar) on Apr 21, 2004 at 03:32 UTC |