use strict; use warnings; use MCE; use MCE::Shared; use Time::HiRes 'time'; my $start = time; my @ten = ( 0 .. 9 ); my $count = MCE::Shared->hash(); MCE->new( max_workers => 4, sequence => [ 1, 10_000_000 ], bounds_only => 1, chunk_size => 50_000, user_func => sub { my ( $mce, $seq, $chunk_id ) = @_; my %lcount; # compute using a local hash - involves zero IPC $lcount{ $ten[ rand @ten ] }++ for $seq->[0] .. $seq->[1]; # increment shared hash - one IPC per key while ( my ( $key, $val ) = each %lcount ) { $count->incrby( $key, $val ); } } )->run; printf "$_ -> %ld\n", $count->get($_) for @ten; printf "duration: %0.3f seconds\n", time - $start;