I'm not really convinced you're testing what you think you're testing. That's because you're attempting to close on the lexical start/middle/end variables, but the string is being eval'd way outside of that scope. That's why, when possible, I prefer to see anonymous code refs instead. So I tried converting your code to that - and, while I was at it, I made a minor optimisation to grap where I didn't copy all the elements to a lexical variable first ("grap2"). And then another minor optimisation to stop copying anything ("grap3").

#! /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 } +, }) }
And the results are much different from yours:
START: Rate grap grep grap2 grap3 first grap 3397/s -- -37% -93% -93% -99% grep 5381/s 58% -- -89% -89% -99% grap2 47287/s 1292% 779% -- -3% -92% grap3 48596/s 1330% 803% 3% -- -92% first 580886/s 16998% 10695% 1128% 1095% -- MIDDLE: Rate grap grap3 grap2 first grep grap 1152/s -- -22% -23% -75% -79% grap3 1486/s 29% -- -0% -67% -72% grap2 1493/s 30% 0% -- -67% -72% first 4567/s 297% 207% 206% -- -15% grep 5381/s 367% 262% 260% 18% -- END: Rate grap grap3 grap2 first grep grap 756/s -- -15% -16% -69% -86% grap3 891/s 18% -- -1% -63% -84% grap2 898/s 19% 1% -- -63% -84% first 2400/s 218% 170% 167% -- -56% grep 5493/s 627% 517% 512% 129% --
Which tells me that the second optimisation I made (grap3 vs grap2) isn't much of one ;-)

However, these are much lower numbers for rates which tells me that they are looping each time. I have a really fast machine already - I don't see how you could loop through 10,000 elements via grep, and do that 2.3 million times per second - that's 23 billion function calls per second, way past believability in any language on a single CPU.


In reply to Re^2: RFC: Text::Grap by Tanktalus
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.