in reply to Re^3: multicolumn extraction
in thread multicolumn extraction
Your reply makes sense, sauoq. I see that I assumed too much by the OP's field representations as not containing any spaces, so it would be best to split on the known field delimiter. Indeed, it would be disastrous if the first field contained spaces. Good catch and thank you for bringing this to my attention.
Update:
I was curious to see whether there was any speed difference between spliting all or spliting some columns, so I ran the following which creates and splits a 20 column x 10000 row file:
use Modern::Perl; use Benchmark qw(cmpthese); my $entry = "aaaaaaaaaaaaaaaaa"; my $columnsFile = 'columns.txt'; open my $file, ">$columnsFile" or die $!; do { print $file "$entry\t" x 19; say $file $entry } for 1 .. 10000; close $file; sub splitAll { open my $file, "<$columnsFile" or die $!; while (<$file>) { my @columns = split /\t/; } close $file; } sub splitSome { open my $file, "<$columnsFile" or die $!; while (<$file>) { my @columns = ( split /\t/ )[ 1 .. 2 ]; } close $file; } cmpthese( -5, { splitAll => sub { splitAll() }, splitSome => sub { splitSome() } } );
Results:
Rate splitAll splitSome splitAll 19.8/s -- -21% splitSome 25.1/s 27% --
In this case, spliting only some shows a significant speed advantage--and with this relatively small file. I ran the script many times, getting as high as 31% for splitSome and as low as 21%--but always showing that splitSome is significantly faster.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: multicolumn extraction
by GrandFather (Saint) on Jun 03, 2012 at 22:33 UTC | |
by Kenosis (Priest) on Jun 03, 2012 at 23:00 UTC | |
by sauoq (Abbot) on Jun 04, 2012 at 20:08 UTC |