The following is a parallel demonstration, based on the code by BrowserUk. There is a bug in MCE::Shared 1.001 and the reason for the length check below. MCE 1.704 and MCE 1.002 will be released in ETA ~ 1 week with the fix.
MCE::Flow and MCE::Shared
use strict;
use warnings;
use MCE::Flow;
use MCE::Shared;
use Time::HiRes qw[ time ];
my $start = time;
my $fh = MCE::Shared->handle( "<:raw", $ARGV[ 0 ] );
my @seen;
sub tally {
my ($aref) = @_;
for ( 0 .. 255 ) {
$seen[$_] += $aref->[$_] if $aref->[$_];
}
return;
}
mce_flow { max_workers => 8 }, sub {
my @_seen;
while( read( $fh, my $buf, 16384 * 4 ) ) {
# the length check may be omitted with MCE::Shared 1.002+
last unless length($buf);
++$_seen[$_] for unpack 'C*', $buf;
}
MCE->do('tally', \@_seen);
};
close $fh;
printf "Took %f secs\n", time() - $start;
# for ( 0 .. 255 ) {
# printf "%c : %u\n", $_, $seen[$_] if $seen[$_];
# }
The serial code takes 8.390 seconds. In comparison, the parallel code completes in 2.253 seconds for a 126 MB file on a machine with 4 real cores and 4 hyper-threads.
Update:
The upcoming MCE::Shared 1.002 release will support the following construction by allowing the main or worker process to handle the error. I've been wanting for the shared open call to feel like the native open call.
use MCE::Shared 1.002;
mce_open my $IN, "<:gzip", "wat.paths.gz" or die "open error: $!";
mce_open my $OUT, ">", \*STDOUT or die "open error: $!";
-
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.