Thanks++, that explains it. After seeing your post, I vaguely recall having seen a "Benchmark gotcha" somewhere that had a similar explanation, but I can't place it.

I normally use subs when I do benchamrks, but this time I figured that the operations being benchmarked were so fast and the differences between them potentially so slight, that the overhead of calling the subs would significantly distort the results. Therefore, I repeated the tests, still using eval'ed strings, but replacing the relevant lexicals with package variables. Here are the results:

# input length: 7 Rate ?<!Y ?!X ?<!X ?!Y ?<!Y 613304/s -- -32% -50% -52% ?!X 908420/s 48% -- -27% -29% ?<!X 1236528/s 102% 36% -- -4% ?!Y 1286220/s 110% 42% 4% --
The number of executions per second indeed goes up significantly (as I expected), but the ratio between the fastest and the slowest goes down, which make no sense to me. So the puzzle is seriously wounded, but not dead yet ;-) .

use strict; use warnings; use Benchmark 'cmpthese'; my $sz = ( shift || 10 ) - 4; our $X = 'N' . 'x' x $sz . '001'; our $Y = 'N' . 'x' x $sz . '000'; print '# input length: ', length( $X ), $/; cmpthese( -1, { '?!X' => '$main::X =~ /^N(?!.*00$).*$/', '?!Y' => '$main::Y =~ /^N(?!.*00$).*$/', '?<!X' => '$main::X =~ /^N.*(?<!00$)$/', '?<!Y' => '$main::Y =~ /^N.*(?<!00$)$/', } ); __END__

the lowliest monk


In reply to Re^2: Benchmarking instability by tlm
in thread Benchmarking instability by tlm

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.