In simple cases there is NO performance gain in using your function or even XS-based first() of List::Util :)
grep in scalar context is much faster :)

UPD: Tanktalus found a bug in my benchmark, so i corrected it :) The first function from List::Util beats the grep-based solution if the "good" element appears in the first middle of the array.

Have a look at the benchmark:

#!/usr/bin/perl use warnings; use strict; use List::Util 'first'; use Benchmark qw(:hireswallclock cmpthese); sub grap (&@) { my ($sub,@list) = @_; foreach (@list) { return 1 if ($sub->($_)); } return; } my $count = 10000; our ($start, $end, $middle); $_ = [(0) x $count] foreach ($start, $end, $middle); $start->[0] = 1; $middle->[($count/2) -1] = 1; $end->[$count-1] = 1; foreach my $name (qw/start middle end/) { print uc "\n$name:\n"; cmpthese ( -1,{ 'grap' => 'my $result = grap { $_ } @$'.$name, 'grep' => 'my $result = grep { $_ } @$'.$name, 'first'=> 'my $result = first { $_ } @$'.$name, }) }
And at its result (the uppercase words denote the position of the searched element in the array):
START: Rate grap grep first grap 638/s -- -14% -99% grep 746/s 17% -- -99% first 57971/s 8983% 7672% -- MIDDLE: Rate grap first grep grap 175/s -- -76% -77% first 735/s 319% -- -3% grep 758/s 332% 3% -- END: Rate grap first grep grap 102/s -- -73% -86% first 377/s 269% -- -50% grep 751/s 635% 99% --

In reply to Re: RFC: Text::Grap by Ieronim
in thread RFC: Text::Grap by kwaping

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.