I just realised that we both did not include in the benchmark the solution that beats first() in all cases—an inlined foreach loop :)

Benchmark code:

#!/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; } sub grap2 (&@) { my $sub = shift; foreach (@_) { return 1 if ($sub->($_)); } return; } sub grap3 (&\@) { my $sub = shift; foreach (@$_[0]) { return 1 if ($sub->($_)); } return; } my $count = 10000; my @data = ( [ start => [ (0) x $count ] ], [ middle => [ (0) x $count ] ], [ end => [ (0) x $count ] ], ); $data[0][1][0] = 1; $data[1][1][$count/2 - 1] = 1; $data[2][1][-1] = 1; foreach my $i (@data) { my $name = $i->[0]; my $array = $i->[1]; print uc "\n$name:\n"; cmpthese (-1, #2_000_000, { 'grap' => sub { my $result = grap { $_ } @$array } +, 'grap2' => sub { my $result = grap2 { $_ } @$array } +, 'grap3' => sub { my $result = grap2 { $_ } @$array } +, 'grep' => sub { my $result = grep { $_ } @$array } +, 'first' => sub { my $result = first { $_ } @$array } +, 'foreach' => sub { my $good; foreach (@$array) { ($good = 1, last) if $_ } } }) }

Results on my machine:

START: Rate grap grep grap2 grap3 first foreach grap 605/s -- -19% -93% -93% -99% -100% grep 745/s 23% -- -91% -91% -99% -100% grap2 8416/s 1291% 1030% -- -2% -84% -99% grap3 8566/s 1316% 1050% 2% -- -84% -99% first 53924/s 8811% 7140% 541% 530% -- -95% foreach 1005403/s 166050% 134888% 11846% 11637% 1764% -- MIDDLE: Rate grap grap2 grap3 first grep foreach grap 169/s -- -31% -32% -77% -78% -84% grap2 244/s 45% -- -2% -67% -68% -77% grap3 249/s 47% 2% -- -67% -68% -76% first 746/s 342% 205% 200% -- -3% -29% grep 772/s 358% 216% 210% 3% -- -27% foreach 1056/s 526% 332% 325% 42% 37% -- END: Rate grap grap3 grap2 first foreach grep grap 99.3/s -- -18% -20% -73% -81% -86% grap3 121/s 22% -- -3% -68% -77% -84% grap2 124/s 25% 3% -- -67% -76% -83% first 372/s 275% 208% 199% -- -29% -49% foreach 526/s 430% 335% 323% 41% -- -28% grep 735/s 640% 508% 491% 98% 40% --

In reply to Re^3: 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.