#! perl -slw use strict; use threads; use threads::shared; use Time::HiRes qw[ sleep time ]; use Text::LevenshteinXS qw(distance); my $minimum_score = 50; my $max_childs = 4; my %contacts :shared; get_db_data( \%contacts ); print "Starting with max: $max_childs concurrent threads"; my $start = time; for my $id ( keys %contacts ) { sleep 0.1 while threads->list >= $max_childs; async( sub { my $source_name = shift; my $counter = 0; for my $contact ( keys %contacts ) { my $dest_name = $contacts{ $contact }; next if $dest_name eq $source_name; my $distance = distance( $source_name, $dest_name ); my $score = 100 - ( $distance * 100 / length( $dest_name ) || 1 ); if( $score >= $minimum_score ) { print "$id looks like $contact"; } ++$counter; } printf "Thread %d compared $id with $counter contacts\n", threads->tid; threads->self->detach; }, $contacts{ $id } ); } sleep 0.1 while threads->list; printf "Took %6f seconds\n", time() - $start; exit; sub get_db_data { ## mock_up my $ref = shift; $ref->{ $_ } = join '', map{ ( 'A', 'C', 'G', 'T' )[ rand 4 ] } 1 .. 1e2 for 1 .. 25; return; } __END__ c:\test>866682 Starting with max: 4 concurrent threads Thread 1 compared 11 with 24 contacts Thread 2 compared 21 with 24 contacts Thread 3 compared 7 with 24 contacts Thread 4 compared 17 with 24 contacts Thread 5 compared 2 with 24 contacts 22 looks like 1 22 looks like 24 Thread 6 compared 22 with 24 contacts 1 looks like 22 Thread 7 compared 1 with 24 contacts Thread 8 compared 18 with 24 contacts Thread 9 compared 23 with 24 contacts Thread 10 compared 16 with 24 contacts Thread 11 compared 13 with 24 contacts Thread 12 compared 25 with 24 contacts 6 looks like 5 Thread 13 compared 6 with 24 contacts 3 looks like 14 Thread 14 compared 3 with 24 contacts Thread 15 compared 9 with 24 contacts Thread 16 compared 12 with 24 contacts Thread 17 compared 20 with 24 contacts 14 looks like 3 Thread 18 compared 14 with 24 contacts Thread 19 compared 15 with 24 contacts Thread 20 compared 8 with 24 contacts Thread 21 compared 4 with 24 contacts 24 looks like 22 Thread 22 compared 24 with 24 contacts Thread 23 compared 19 with 24 contacts Thread 24 compared 10 with 24 contacts 5 looks like 6 Thread 25 compared 5 with 24 contacts Took 0.395703 seconds