Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
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?
[marioroy]: Lady_Aleena Yes, will post something with all the new features. Parallel with few lines of code.
[marioroy]: Yes, will do for PM. I love PM.
Lady_Aleena ponders a meditation of her own, but it would be on a far far less important topic.
[marioroy]: I'm hoping to have a release in about a week's time.
Discipulus supermario..
marioroy MCE isn't important by any means. Just wanted to complete this task. Hobo needed more love and MCE::Shared as well. It wasn't easy at all.
[marioroy]: Discipulus No supermario. I'm a humbled person within. Just wanted to complete this journey.

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2017-05-26 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?