Nice, doing the least amount of work is the fastest

#!/usr/bin/perl -- use Benchmark ':all'; my $orig = '105 106 107 108 109 110 111 112 113 220 221 223 100 101 198 199 200 201 298 299 300 301 398 399 400 401 1115 1116 1117 1118 1119 1120 1121 1122 1123 1200 1201 1202 1100 1101 1102 1198 1199 1200 1201 1202 1298 1299 1300 1301 12345 12346 12347 12348 12349 12450 12451 12453 12466 12467 12300 12301 12398 12399 12400 12401 12498 12499 12500 12501';;; print "$]\n"; exit Verify() if @ARGV; cmpthese( -3, { anoNa => \&anoNa , anoNb => \&anoNb , anoM => \&anoM , bukA => \&bukA , bukB => \&bukB , } ); sub anoNa { $_ = $orig; s[(\d+)\d\K\s(?=(??{$1+1})\d\s)]{\n}g; } sub anoNb { $_ = $orig; s[(?<=\s)(\d+)\d\K\s(?=(??{$1+1})\d\s)]{\n}g; +} sub anoM { $_ = $orig; s{ (\d) \d\d \K [^\n\S]+ (?! \d* \1 \d\d \b) + }{\n}xmsg; } sub bukA { $_ = $orig; s[(?<=\s)(\d+)\d\K\s(?=(\d+)\d\s)]{$1+1==$2 ? +qq[\n] : ' '}ge; } sub bukB { $_ = $orig; s[\b(\d+)\d\K\s(?=(\d+)\d\s)]{$1+1==$2 ? qq[\n +] : ' '}ge; } sub Verify { print "\n# anoNa \n", anoNa(),"\n"; print "$_\n\n"; print "\n# anoNb \n", anoNb(),"\n"; print "$_\n\n"; print "\n# anoM \n", anoM(),"\n"; print "$_\n\n"; print "\n# bukA \n", bukA(),"\n"; print "$_\n\n"; print "\n# bukB \n", bukB(),"\n"; print "$_\n\n"; } __END__
5.016003 Rate anoNa anoNb bukA bukB anoM anoNa 461/s -- -59% -90% -90% -94% anoNb 1126/s 144% -- -76% -76% -85% bukA 4662/s 910% 314% -- -0% -40% bukB 4675/s 913% 315% 0% -- -40% anoM 7751/s 1580% 588% 66% 66% --
FWIW the outputs aren't identical but they're close enough :)

In reply to Re^2: Why doesn't this regex work? (?!bench) by Anonymous Monk
in thread Why doesn't this regex work? (Solved!) by BrowserUk

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.