I think your data set is way too small (four elements), and you don't account for differences you might get from matching a different number of times.

When benchmarking a list operation, try it with a long list! If you're testing some kind of selection method, see how it works against different proportions of matches. Which is faster when there's nothing to find? Which is faster when everything matches?

I tried these out, and I was a little surprised at the results. Basically grep wins when nothing matches, but for wins when everything matches.

Mostly, though, I'm not sure this matters. A difference as small as that is likely just noise. After all, when I ran this a second time, for came out ahead in the non-matching scenario.

use List::Util qw( shuffle ); my @match_0 = gen_values( 0 ); my @match_25 = gen_values( 1/4 ); my @match_50 = gen_values( 1/2 ); my @match_75 = gen_values( 3/4 ); my @match_100 = gen_values( 1 ); sub gen_values { my $proportion = shift; my $count = 10_000; my $matching = $count * $proportion; my @out; push @out, gen_matching() while $matching-- > 0; push @out, gen_non_matching() while scalar @out < $count; return shuffle @out; } sub gen_matching { my $out = gen_non_matching(); substr( $out, rand length $out, 1 ) = 'x'; return $out; } sub gen_non_matching { my $out = ''; $out .= int rand 10 while length $out < 100; return $out; } use Benchmark qw( cmpthese ); cmpthese -2, { for_0 => sub { for_loop( \@match_0 ) }, for_25 => sub { for_loop( \@match_25 ) }, for_50 => sub { for_loop( \@match_50 ) }, for_75 => sub { for_loop( \@match_75 ) }, for_100 => sub { for_loop( \@match_100 ) }, grep_0 => sub { grep_loop( \@match_0 ) }, grep_25 => sub { grep_loop( \@match_25 ) }, grep_50 => sub { grep_loop( \@match_50 ) }, grep_75 => sub { grep_loop( \@match_75 ) }, grep_100 => sub { grep_loop( \@match_100 ) }, }; sub for_loop { my @values = @{shift()}; my @matches; for ( @values ) { push @matches, $_ if /x/; } return @matches; } sub grep_loop { my @values = @{shift()}; my @matches = grep /x/, @values; return @matches; } __END__ Rate grep_100 grep_75 for_100 grep_50 for_75 for_50 grep_2 +5 for_25 for_0 grep_0 grep_100 81.9/s -- -11% -18% -20% -25% -30% -31 +% -36% -42% -43% grep_75 92.0/s 12% -- -8% -10% -15% -21% -22 +% -28% -34% -36% for_100 100.0/s 22% 9% -- -2% -8% -14% -15 +% -22% -29% -30% grep_50 102/s 25% 11% 2% -- -6% -12% -13 +% -20% -27% -29% for_75 108/s 32% 18% 9% 6% -- -7% -8 +% -15% -23% -25% for_50 117/s 42% 27% 17% 14% 7% -- -1 +% -9% -17% -19% grep_25 118/s 44% 28% 18% 15% 9% 1% - +- -7% -16% -18% for_25 128/s 56% 39% 28% 25% 18% 9% 8 +% -- -9% -11% for_0 140/s 71% 52% 40% 37% 29% 20% 19 +% 10% -- -3% grep_0 144/s 76% 56% 44% 41% 33% 23% 22 +% 13% 3% --

In reply to Re: Battle Royal Grep vs For by kyle
in thread Battle Royal Grep vs For by Herkum

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.