In this case, the firstidx function in List::MoreUtils may be a better fit, because it provides a simpler syntax:

my $i1 = firstidx { $_ == 1 } @array;

And it’s faster too:

use strict; use warnings; use feature qw( state ); use Benchmark qw( cmpthese ); use List::Util qw( first shuffle ); use List::MoreUtils qw( firstidx indexes ); my @array = shuffle (1 .. 1e6); cmpthese ( 10, { grep => sub { _grep() }, first => sub { _first() }, indexes => sub { _indexes() }, firstidx => sub { _firstidx() }, } ); sub _grep { state $init = 1; my ($i1) = grep { $array[$_] == 1 } 0 .. $#array; my ($i2) = grep { $array[$_] == 2 } 0 .. $#array; my ($i3) = grep { $array[$_] == 3 } 0 .. $#array; if ($init) { printf "<%d> <%d> <%d>\n", $i1, $i2, $i3; $init = 0; } } sub _first { state $init = 1; my $i1 = first { $array[$_] == 1 } (0 .. $#array); my $i2 = first { $array[$_] == 2 } (0 .. $#array); my $i3 = first { $array[$_] == 3 } (0 .. $#array); if ($init) { printf "<%d> <%d> <%d>\n", $i1, $i2, $i3; $init = 0; } } sub _indexes { state $init = 1; my $i1 = indexes { $array[$_] == 1 } (0 .. $#array); my $i2 = indexes { $array[$_] == 2 } (0 .. $#array); my $i3 = indexes { $array[$_] == 3 } (0 .. $#array); if ($init) { printf "<%d> <%d> <%d>\n", $i1, $i2, $i3; $init = 0; } } sub _firstidx { state $init = 1; my $i1 = firstidx { $_ == 1 } @array; my $i2 = firstidx { $_ == 2 } @array; my $i3 = firstidx { $_ == 3 } @array; if ($init) { printf "<%d> <%d> <%d>\n", $i1, $i2, $i3; $init = 0; } }

Output:

23:20 >perl 1940_SoPW.pl <748548> <843435> <178789> <748548> <843435> <178789> <748548> <843435> <178789> <748548> <843435> <178789> Rate grep indexes first firstidx grep 1.19/s -- -1% -34% -52% indexes 1.20/s 1% -- -34% -51% first 1.81/s 53% 51% -- -27% firstidx 2.47/s 108% 106% 37% -- 23:20 >

Cheers,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^2: grep surprise by Athanasius
in thread grep surprise by morgon

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.