Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I think smls might be on the right track with moving pos. This benchmark seems to show that there is, if anything, a performance gain. I tried a modification to my first attempt my removing the duplicate matches via a hash but it only showed a marginal improvement in performance.

Then again, I am not very good at benchmarks so I could have cocked it up :-/

use strict; use warnings; use 5.014; use Benchmark qw{ cmpthese }; my $n = 4; my $str = q{x} x 50; substr $str, $_, 4, q{fred} for 4, 9, 20, 24, 31, 40; say qq{String: $str\n}; my $rcMovePos = sub { my $raMatches; while ( $str =~ m{\G(?:.{$n})*?(?=(fred.*))}g ) { push @{ $raMatches }, [ pos( $str ), $1 ]; pos $str += $n; } return $raMatches; }; my $rcNoDups = sub { my $rhMatches; $rhMatches->{ pos( $str ) } = $1 while $str =~ m{\G(?:.{$n})*?(?=(fred.*))}g; return $rhMatches; }; my $rcWithDups = sub { my $raMatches; push @{ $raMatches }, [ pos( $str ), $1 ] while $str =~ m{\G(?:.{$n})*?(?=(fred.*))}g; return $raMatches; }; my $raRes = $rcMovePos->(); say q{Using $rcMovePos}; say qq{ Matched $_->[ 1 ] at position $_->[ 0 ]} for @{ $raRes }; my $rhRes = $rcNoDups->(); say q{Using $rcNoDups}; say qq{ Matched $rhRes->{ $_ } at position $_} for sort { $a <=> $b } keys %{ $rhRes }; $raRes = $rcWithDups->(); say q{Using $rcWithDups}; say qq{ Matched $_->[ 1 ] at position $_->[ 0 ]} for @{ $raRes }; srand 1234567890; $str = q{x} x 10000; substr $str, int rand 9997, 4, q{fred} for 1 .. 50; say q{}; cmpthese( -5, { movePos => $rcMovePos, noDups => $rcNoDups, withDups => $rcWithDups, } );
String: xxxxfredxfredxxxxxxxfredfredxxxfredxxxxxfredxxxxxx Using $rcMovePos Matched fredxfredxxxxxxxfredfredxxxfredxxxxxfredxxxxxx at position 4 Matched fredfredxxxfredxxxxxfredxxxxxx at position 20 Matched fredxxxfredxxxxxfredxxxxxx at position 24 Matched fredxxxxxx at position 40 Using $rcNoDups Matched fredxfredxxxxxxxfredfredxxxfredxxxxxfredxxxxxx at position 4 Matched fredfredxxxfredxxxxxfredxxxxxx at position 20 Matched fredxxxfredxxxxxfredxxxxxx at position 24 Matched fredxxxxxx at position 40 Using $rcWithDups Matched fredxfredxxxxxxxfredfredxxxfredxxxxxfredxxxxxx at position 4 Matched fredxfredxxxxxxxfredfredxxxfredxxxxxfredxxxxxx at position 4 Matched fredfredxxxfredxxxxxfredxxxxxx at position 20 Matched fredfredxxxfredxxxxxfredxxxxxx at position 20 Matched fredxxxfredxxxxxfredxxxxxx at position 24 Matched fredxxxfredxxxxxfredxxxxxx at position 24 Matched fredxxxxxx at position 40 Matched fredxxxxxx at position 40 Rate withDups noDups movePos withDups 2321/s -- -3% -33% noDups 2394/s 3% -- -31% movePos 3445/s 48% 44% --

I hope this is of interest.

Cheers,

JohnGG


In reply to Re: A regex that only matches at offset that are multiples of a given N? by johngg
in thread A regex that only matches at offset that are multiples of a given N? 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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (9)
    As of 2014-12-23 03:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (133 votes), past polls