$ perl demo.pl haukex 50 duration (haukex): 1.236 seconds found match: yes $ perl demo.pl karlary 50 duration (karlary): 0.825 seconds found match: yes $ perl demo.pl karlseq 50 duration (karlseq): 0.313 seconds found match: yes #### $ perl demo.pl haukex 50 duration (haukex): 17.388 seconds found match: yes $ perl demo.pl karlary 50 duration (karlary): 7.633 seconds found match: yes $ perl demo.pl karlseq 50 duration (karlseq): 2.858 seconds found match: yes #### #!/usr/bin/env perl use strict; use warnings; use feature qw( say ); use MCE::Loop; use Time::HiRes qw( time ); sub usage { warn "usage: $0 ( haukex | karlary | karlseq ) [ count ]\n\n"; exit 1; } my $func = shift || usage(); my $count = shift || 50; usage() unless main->can($func); my $cpus = MCE::Util->get_ncpu() || 4; my $max = 100000; MCE::Loop::init { max_workers => $cpus, chunk_size => 8000, # <-- do not go over 8000 bounds_only => 1 # <-- applies to sequence }; my $data = [ 'AGCTCGTTGTTCGATCCA', 'GAGAGATAGATGATAGTG', 'TTTT_CCCC', 0 ]; our %barcode_hash = map { $_ => $data } 1 .. $max - 2; $barcode_hash{ ($max - 1) } = [ 'AGCTCGTTGTTCGATCCA', 'GAGAGATAGATGATAGTG', 'TTTT_AAAA', 0 ]; $barcode_hash{ ($max) } = [ 'AGCTCGTTGTTCGATCCA', 'GAGAGATAGATGATAGTG', 'TTTT_AAAA', 0 ]; our $barcode_pair_35 = 'TTTT_AAAA'; { no strict 'refs'; my $start = time; my $ret; $ret = $func->() for 1 .. $count; printf "duration ($func): %0.03f seconds\n", time - $start; printf "found match: %s\n", $ret ? 'yes' : 'no'; } exit 0; sub haukex { # serial code my $ret = 0; for ( 1 .. $max ) { $ret = 1, last if $barcode_hash{$_}[2] eq $barcode_pair_35; } return $ret; } sub karlary { # workers receive next array chunk my @ret = mce_loop { my ( $mce, $chunk_ref, $chunk_id ) = @_; for ( @$chunk_ref ) { MCE->gather(1), MCE->abort(), last if ( $barcode_hash{$_}[2] eq $barcode_pair_35 ); } } 1 .. $max; # <-- for array 1 .. $max return @ret ? 1 : 0; } sub karlseq { # workers receive next sequence 'begin' and 'end' boundaries my @ret = mce_loop_s { my ( $mce, $chunk_ref, $chunk_id ) = @_; for ( $chunk_ref->[0] .. $chunk_ref->[1] ) { MCE->gather(1), MCE->abort(), last if ( $barcode_hash{$_}[2] eq $barcode_pair_35 ); } } 1, $max; # <-- for sequence 1, $max return @ret ? 1 : 0; } #### $ perl demo.pl haukex 50 duration (haukex): 1.232 seconds found match: yes $ perl demo.pl karlary 50 duration (karlary): 1.482 seconds found match: yes $ perl demo.pl karlseq 50 duration (karlseq): 0.858 seconds found match: yes #### $ perl demo.pl haukex 50 duration (haukex): 20.108 seconds found match: yes $ perl demo.pl karlary 50 duration (karlary): 16.770 seconds found match: yes $ perl demo.pl karlseq 50 duration (karlseq): 11.419 seconds found match: yes #### $ perl demo.pl haukex 50 duration (haukex): 1.607 seconds found match: yes $ perl demo.pl karlary 50 duration (karlary): 1.529 seconds found match: yes $ perl demo.pl karlseq 50 duration (karlseq): 0.749 seconds found match: yes #### $ perl demo.pl haukex 50 duration (haukex): 25.194 seconds found match: yes $ perl demo.pl karlary 50 duration (karlary): 14.446 seconds found match: yes $ perl demo.pl karlseq 50 duration (karlseq): 7.051 seconds found match: yes