In your any and any_cr branches you are running any twice when you only need to run it once. If I fold the two together like this:

any_fold => sub { while ( $data =~ /^(\d+) (\d+)/mg ) { next if any { $1 == $_ || $2 == $_ } @skip; } return 1 }, any_cr_fold => sub { while ( $data =~ /^(\d+) (\d+)/mg ) { my ( $c, $r ) = ( $1, $2 ); next if any { $c == $_ || $r == $_ } @skip; } return 1 },

I get these results:

any_cr 461/s -- -13% -58% -62% -68% + -76% any_cr_fold 527/s 14% -- -52% -57% -63% + -72% any 1090/s 137% 107% -- -11% -24% + -43% any_fold 1221/s 165% 132% 12% -- -15% + -36% ugly 1436/s 212% 172% 32% 18% -- + -25% ugly_cr 1914/s 315% 263% 76% 57% 33% + --

Then, since I am on 5.42.0 and can use the new any keyword (suggested by ysth) by replacing your use of List::Util with:

no warnings "experimental::keyword_any"; use experimental 'keyword_any';

it gives these even better results:

Rate any any_fold any_cr ugly any_cr_fold + ugly_cr any 1037/s -- -10% -20% -26% -31% + -46% any_fold 1152/s 11% -- -11% -18% -23% + -40% any_cr 1297/s 25% 13% -- -7% -14% + -32% ugly 1399/s 35% 21% 8% -- -7% + -27% any_cr_fold 1506/s 45% 31% 16% 8% -- + -21% ugly_cr 1914/s 85% 66% 48% 37% 27% + --

ugly_cr still wins but not by so much. But what's really interesting to me is that with List::Util, any_cr is much slower than any while with the new keyword, any_cr is slightly faster than any.


🦛


In reply to Re: Why is "any" slow in this case? by hippo
in thread Why is "any" slow in this case? by Anonymous Monk

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.