Hello John FENDER,
The following is a parallel demonstration using MCE::Flow and MCE::Shared.
use strict; use warnings; use MCE::Flow; use MCE::Shared; open my $fh, "unzip -p 10-million-combos.zip |" or die "$!"; my $counter1 = MCE::Shared->scalar( 0 ); my $counter2 = MCE::Shared->scalar( 0 ); mce_flow { chunk_size => '1m', max_workers => 8, use_slurpio => 1, }, sub { my ( $mce, $chunk_ref, $chunk_id ) = @_; my ( $numlines, $occurances ) = ( 0, 0 ); while ( $$chunk_ref =~ /([^\n]+\n)/mg ) { $numlines++; $occurances++ if ( $1 =~ /123456\r/ ); } $counter1->incrby( $numlines ); $counter2->incrby( $occurances ); }, $fh; close $fh; print "Num lines : ", $counter1->get(), "\n"; print "Occurances: ", $counter2->get(), "\n";
The following construction reads the plain text file directly if already unzipped.
use strict; use warnings; use MCE::Flow; use MCE::Shared; my $counter1 = MCE::Shared->scalar( 0 ); my $counter2 = MCE::Shared->scalar( 0 ); mce_flow_f { chunk_size => '1m', max_workers => 8, use_slurpio => 1, }, sub { my ( $mce, $chunk_ref, $chunk_id ) = @_; my ( $numlines, $occurances ) = ( 0, 0 ); while ( $$chunk_ref =~ /([^\n]+\n)/mg ) { $numlines++; $occurances++ if ( $1 =~ /123456\r/ ); } $counter1->incrby( $numlines ); $counter2->incrby( $occurances ); }, "10-million-combos.txt"; print "Num lines : ", $counter1->get(), "\n"; print "Occurances: ", $counter2->get(), "\n";
In reply to Re: How to optimize a regex on a large file read line by line ?
by marioroy
in thread How to optimize a regex on a large file read line by line ?
by John FENDER
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |