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, }) }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |