I don't know about any efficiency issues though.

Doesn't look good:

#!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% --

And thats using the XS List::Util implementation from 5.8.2. Theres a lot of overhead in calling the closure, and I think that the range operator isn't lazy in this usage, so it has to manufacture a list of indexes to use.


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi



In reply to Re^3: Common Perl Idioms by demerphq
in thread Common Perl Idioms by eric256

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.