Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

LanceDeeply's scratchpad

by LanceDeeply (Chaplain)
on Jun 02, 2004 at 00:20 UTC ( #358886=scratchpad: print w/ replies, xml ) Need Help??

LanceDeeply's scratchpad
use strict; use warnings; use Data::Dumper; use threads; my $slow_matches_b = sub { sleep 1; return unless $_[0]; return 1 if $_[0] =~ /b/; }; my $test_strings = [ ('blee','blah','bloo', 'qoo', 'fwee' ) ]; parallel_map($slow_matches_b,$test_strings); sub serial_map { my $function = shift; my $data = shift; my @results = map { $slow_matches_b->($_) } @$test_strings; print Dumper \@results; } sub parallel_map { my $function = shift; my $data = shift; # # assign keys to each data element # my %hash_input; for ( 0..( @$data-1) ) { $hash_input{$_} = $$data[$_]; } # # run each data element in separate thread # my @threads; for ( keys %hash_input ) { my $t = threads->create( sub { map_element($_, $function, $has +h_input{$_} ) } ); push @threads, $t; } # # wait for threads to return ( this implementation is bound by +slowest thread ) # my %results = map { %{$_->join()}; } @threads; print Dumper \%results; } sub map_element { my $key = shift; my $function = shift; my $data = shift; return { $key => $function->($data) }; }
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2015-07-02 23:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (47 votes), past polls