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

Re: reading lines into an array

by BrowserUk (Pope)
on Jun 11, 2013 at 03:42 UTC ( #1038188=note: print w/replies, xml ) Need Help??


in reply to reading lines into an array

If performance is in any way a criteria, don't use File::Slurp. And if the size of the file is any more than a few hundred lines, don't use list assignment either:

#! perl -slw use strict; use File::Slurp; use Benchmark qw[ cmpthese ]; print `wc -l $ARGV[ 0 ]`; open our $infile, '<', $ARGV[ 0 ] or die $!; our $I //= -1; cmpthese $I, { a=> q[ sysseek $infile, 0, 0; my @lines = read_file( $infile, chomp => 1 ); print 'a: ', scalar @lines if $I == 1; ], b=> q[ sysseek $infile, 0, 0; chomp( my @lines = <$infile> ); print 'b: ', scalar @lines if $I == 1; ], c=> q[ sysseek $infile, 0, 0; my @lines; $lines[ @lines ] = <$infile> until eof $infile; chomp @lines; print 'c: ', scalar @lines if $I == 1; ], }; __END__ C:\test>t-slurp -I=1 small.tsv 1000000 small.tsv a: 1000000 (warning: too few iterations for a reliable count) b: 1000000 (warning: too few iterations for a reliable count) c: 1000000 (warning: too few iterations for a reliable count) s/iter a b c a 257 -- -98% -98% b 4.94 5115% -- -16% c 4.14 6119% 19% -- C:\test>t-slurp -I=-1 small.tsv 1000000 small.tsv (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) s/iter b a c b 4.88 -- -4% -100% a 4.69 4% -- -100% c 1.39e-006 350525381% 337007680% --

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: reading lines into an array
by fionbarr (Friar) on Jun 11, 2013 at 12:29 UTC
    thanks all; thoughtful replies.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1038188]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2016-12-06 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (117 votes). Check out past polls.