Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 exploiting the Monastery: (8)
As of 2014-09-17 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (57 votes), past polls