Hello rsFalse,

First, the code you have does not appear to be producing correct results. For example, you have this line of input:

()(())()

which produces:

8 1

but by inspection we can see that the longest subsequence of validly-nested parentheses is (()) which is only 4 characters long.

Second, as Eily says you can get a significant speedup by using (?PARNO). For example, adapting the code in Can-I-use-Perl-regular-expressions-to-match-balanced-text of perlfaq6, I came up with this:

use strict; use warnings; use List::Util 'max'; srand; my @lines = <DATA>; my $n = 1000; my $long = ''; $long .= qw{( )}[ int(rand() * 2) ] for 1 .. $n; my $regex = qr{ ( # start of capture group 1 \( # match an opening parenthesis (?: [^()]++ # one or more non-parentheses, non backtra +cking | # OR (?1) # recurse to capture group 1 )* \) # match a closing parenthesis ) # end of capture group 1 }x; for my $string (@lines, $long) { my $max = 0; my $occ = 1; my @groups = $string =~ m/$regex/g; if (@groups) { @groups = sort { length $a <=> length $b } @groups; $max = length $groups[-1]; for my $i (reverse 0 .. $#groups - 1) { if (length $groups[$i] == length $groups[-1]) { ++$occ; } else { last; } } } print "$max $occ\n"; } __DATA__ )((())))(()()) ))( ()(())() ((((()((( (()())()(())()()())())()((()(()(())()()())((()(())()(()()()()))()(())( +)(((()())()(()((())()(())(()))

Typical output:

23:28 >perl 1071_SoPW.pl 6 2 0 1 4 1 2 1 20 1 266 1 23:28 >

For a long string this is much faster than using (??{ code }).

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: how to speed up that dynamic regex? by Athanasius
in thread how to speed up that dynamic regex? by rsFalse

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.