in reply to foreach (@array) s/x/y/ efficiency

Your first basic optimization is "compile each regex only once". So get thee over to perlre and learn about qr.

Once you've tackled that, if it's still not fast enough, you can probably turn your algorithm into a state machine so that you don't generate redundant B elements in the first place.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: foreach (@array) s/x/y/ efficiency
by gryphon (Abbot) on Jan 11, 2001 at 03:28 UTC

    To say that this made things extremely faster would be a complete understatement. After being blown away at first, I ran a pseudo-benchmark, and noted that it cut my run time (on that section of code only) by a factor of at least 14! Thank you!

    Just for the sake of my personal education, here is what I came up with based on your suggestion. Did I get it right? Or is there an even better way?

    foreach $phrase (@key_phrases) { my($worda,$wordb) = split(/ /, $phrase); my $pattern = qr/(\W|\b)($worda)(\W+)($wordb)(\W|\b)/; foreach (@material) { last if (s,$pattern,$1<U>$2</U>$3<U>$4</U>$5,i); } }

    Thanks again for your help and wisdom, great one.

    -Gryphon.