#!perl -l use Benchmark qw(cmpthese); use List::Util 'first'; sub find_first_index_dmq { my $target=shift; push @_,$target; my $i=0; $i++ while $_[$i] ne $target; return $i==$#_ ? undef : $i } sub find_first_index_ihb { my $target=shift; first { $_[$_] eq $target } 0 .. $#_ } # 0 1 2 3 4 5 6 7 8 9 0 1 print 'ihb:',find_first_index_ihb(qw(a b c d e f g h a b c d e)); # 0 1 2 3 4 5 6 7 8 9 0 1 print 'ihb:',find_first_index_ihb(qw(a b c d e f g h b c d e)); # 0 1 2 3 4 5 6 7 8 9 0 1 print 'dmq:',find_first_index_dmq(qw(a b c d e f g h a b c d e)); # 0 1 2 3 4 5 6 7 8 9 0 1 print 'dmq:',find_first_index_dmq(qw(a b c d e f g h b c d e)); cmpthese -1,{ dmq_hit=>'find_first_index_dmq(qw(a b c d e f g h a b c d e));', dmq_zip=>'find_first_index_dmq(qw(a b c d e f g h q b c d e));', ihb_hit=>'find_first_index_ihb(qw(a b c d e f g h a b c d e));', ihb_zip=>'find_first_index_ihb(qw(a b c d e f g h q b c d e));', }; __END__ #### Rate ihb_zip ihb_hit dmq_zip dmq_hit ihb_zip 8461/s -- -5% -44% -50% ihb_hit 8869/s 5% -- -42% -47% dmq_zip 15177/s 79% 71% -- -10% dmq_hit 16782/s 98% 89% 11% --