in reply to Re: Re: Finding all substrings
in thread Finding all substrings

Says samtregar:
Um, cool! But it leaks memory at an insane rate on 5.6.1.
Jeff Pinyan explained this one to me. The code in the regex captures the first instance of @ss, and keeps appending to it, 1023 new elements every time through the loop. You can fix the problem by using a static variable:
{ my @ss; sub substrings { @ss = (); $_[0] =~ /.*?(.+?)(?{push @ss, $1})(?!)/; @ss; } }

--
Mark Dominus
Perl Paraphernalia

Replies are listed 'Best First'.
Re: Re: Finding all substrings
by samtregar (Abbot) on Apr 24, 2002 at 21:28 UTC
    Ah, much better. Now I can test it! Here are the results from my benchmark against /usr/dict/words, 100,000 iterations (my final code above is "new" and your code is "mjd"):
    Benchmark: timing 100000 iterations of mjd, new, original... mjd: 17 wallclock secs (16.86 usr + 0.00 sys = 16.86 CPU) @ 59 +31.20/s (n=100000) new: 3 wallclock secs ( 2.70 usr + 0.01 sys = 2.71 CPU) @ 36 +900.37/s (n=100000) original: 17 wallclock secs (12.87 usr + 0.01 sys = 12.88 CPU) @ 77 +63.98/s (n=100000) Rate mjd original new mjd 5931/s -- -24% -84% original 7764/s 31% -- -79% new 36900/s 522% 375% --
    It looks like this solution is actually slower than the original...

    -sam