Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
Welcome to the Monastery
 
PerlMonks  

push, assign or split a file?

by gam3 (Curate)
on Jan 17, 2010 at 03:06 UTC ( #817809=perlquestion: print w/ replies, xml ) Need Help??
gam3 has asked for the wisdom of the Perl Monks concerning the following question:

I was looking into Why is "for" much slower than "while"? and it occured to me that the foreach is doing something very close to @bob = <IN>. This led me to wonder if the while was fast enough to make up for the the expected time lost in allocating memory for the list while pushing elements onto it.

First let me show some benchmarks that demonstrate that it really is faster to use the while statment in place of a simple assignment. However using [ <IN> ] seems to be as efficient as the while loop.

/dev/null
          Rate while array  read  list
while 566972/s    --   -5%  -13%  -16%
array 597140/s    5%    --   -8%  -11%
read  650963/s   15%    9%    --   -3%
list  672405/s   19%   13%    3%    --

/usr/share/dict/words
        Rate  list  read array while
list  6.76/s    --  -32%  -32%  -33%
read  9.88/s   46%    --   -0%   -2%
array 9.92/s   47%    0%    --   -1%
while 10.1/s   49%    2%    1%    --

/etc/passwd
         Rate  list while array  read
list  21806/s    --   -2%  -19%  -31%
while 22357/s    3%    --  -17%  -29%
array 26906/s   23%   20%    --  -15%
read  31658/s   45%   42%   18%    --

/opt/temp
      s/iter  list  read array while
list    1.79    --  -25%  -29%  -38%
read    1.34   34%    --   -4%  -17%
array   1.28   40%    4%    --  -13%
while   1.12   61%   20%   15%    --
Now that read is not cheating, the results seem more reasonable.

It just does not make sense to me that: push(@bob, $_) while <$IN>; would be faster than @bob = <$IN>.

#!/usr/bin/perl use strict; use Benchmark qw( cmpthese ); use Data::Dumper; foreach my $file qw ( /dev/null /usr/share/dict/words /etc/passwd /opt +/temp) { open my $IN1, '<', $file or die "could not open $file"; my @list = <$IN1>; seek( $IN1, 0, 0 ); my $dl = length( Dumper \@list ); print "$file\n"; cmpthese( -10, { while => sub { seek( $IN1, 0, 0 ); my @bob = (); while (<$IN1>) { push @bob, $_; } # my $x = Dumper \@bob; # die unless length($x) == $dl; # die @bob . ' ' . @list unless @bob == @list; }, list => sub { seek( $IN1, 0, 0 ); my @bob = <$IN1>; # my $x = Dumper \@bob; # die unless length($x) == $dl; # die @bob . ' ' . @list unless @bob == @list; }, array => sub { seek( $IN1, 0, 0 ); my $bob = [<$IN1>]; # my $x = Dumper $bob; # die unless length($x) == $dl; # die @$bob . ' ' . @list unless @$bob == @list; }, read => sub { seek( $IN1, 0, 0 ); read $IN1, my $bob, -s $IN1; my @bob = split( /^/m, $bob ); # my $x = Dumper \@bob; # die @bob . ' ' . @list unless @bob == @list; # die length($x), ' ', $dl unless length($x) == $dl; }, } ); }
UPDATE: I left the Dumper line out of the read test -- thanks chromatic. This explains why read was so fast. It is slower now.

I have removed Data::Dumper from the benechmark as well, as it did not change the results and slowed everything down.

-- gam3
A picture is worth a thousand words, but takes 200K.

Comment on push, assign or split a file?
Download Code
Re: push, assingn or split a file?
by ikegami (Pope) on Jan 17, 2010 at 07:53 UTC
    Nits:
    seek( $IN1, 0, SEEK_END ); my $length = tell($IN1); seek( $IN1, 0, 0 );

    can be replaced by -s.

    split( /\n/, $bob );

    is not equivalent. It removes the newline from each element, and it removes trailing blank lines.

      Thanks. The -s is handy. My C is showing.

      To fix the split you need to use /^/m or just /^/ as split will know what you mean.

      -- gam3
      A picture is worth a thousand words, but takes 200K.
Re: push, assign or split a file?
by chromatic (Archbishop) on Jan 17, 2010 at 08:12 UTC
    First let me show some benchmarks that demonstrate that it really is faster to use the while statement in place of a simple assignment.

    Your spurious Data::Dumper calls in three of the four subs ruin the benchmark.

      The reason that I put the Data::Dumper calls in was to make sure that the ARRAY was really being put into memory and not being delayed. If all of the tests had the Data::Dumper code, it should not affect the outcome.

      I have now made it so that all the tests don't use Data::Dumper.

      -- gam3
      A picture is worth a thousand words, but takes 200K.
        If all of the tests had the Data::Dumper code, it should not affect the outcome.

        Indeed it will; if you want to measure IO for reading, don't add IO for writing. IO is too non-deterministic at this level to benchmark accurately and appropriately. (That's even without Benchmark's flaws.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://817809]
Approved by biohisham
Front-paged by biohisham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-04-18 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (462 votes), past polls