Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to push, assign or split a file? by gam3

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found