in reply to Re: Count byte/character occurrence (quickly)
in thread Count byte/character occurrence (quickly)
The following are parallel demonstrations using MCE::Hobo and threads.
MCE::Hobo and MCE::Shared
A Hobo is a migratory worker inside the machine that carries the asynchronous gene. Hobos are equipped with threads-like capability for running code asynchronously. Unlike threads, each hobo is a unique process to the underlying OS. The IPC is managed by MCE::Shared, which runs on all the major platforms including Cygwin.
use strict; use warnings; use MCE::Hobo; use MCE::Shared; use Time::HiRes qw[ time ]; my $start = time; my $fh = MCE::Shared->handle( "<:raw", $ARGV[ 0 ] ); my $seen = MCE::Shared->array; sub task { 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; } for ( 0 .. 255 ) { $seen->incrby($_, $_seen[$_]) if $_seen[$_]; } } MCE::Hobo->create('task') for 1 .. 8; # do other stuff if desired $_->join for MCE::Hobo->list; close $fh; printf "Took %f secs\n", time() - $start; # export and destroy the shared array into a local non-shared array $seen = $seen->destroy; # for ( 0 .. 255 ) { # printf "%c : %u\n", $_, $seen->[$_] if $seen->[$_]; # }
threads and MCE::Shared
The code for MCE::Hobo and threads are very similar.
use strict; use warnings; use threads; use MCE::Shared; use Time::HiRes qw[ time ]; my $start = time; my $fh = MCE::Shared->handle( "<:raw", $ARGV[ 0 ] ); my $seen = MCE::Shared->array; sub task { 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; } for ( 0 .. 255 ) { $seen->incrby($_, $_seen[$_]) if $_seen[$_]; } } threads->create('task') for 1 .. 8; # do other stuff if desired $_->join for threads->list; close $fh; printf "Took %f secs\n", time() - $start; # export and destroy the shared array into a local non-shared array $seen = $seen->destroy; # for ( 0 .. 255 ) { # printf "%c : %u\n", $_, $seen->[$_] if $seen->[$_]; # }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Count byte/character occurrence (quickly)
by james28909 (Deacon) on Apr 01, 2016 at 16:26 UTC | |
by james28909 (Deacon) on Apr 04, 2016 at 22:03 UTC | |
by marioroy (Prior) on Apr 05, 2016 at 07:57 UTC | |
by james28909 (Deacon) on Apr 05, 2016 at 12:26 UTC |