Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: tight loop regex optimization

by moritz (Cardinal)
on Nov 01, 2011 at 07:09 UTC ( #935046=note: print w/replies, xml ) Need Help??

in reply to tight loop regex optimization

You are using the $& special variable in your program. perlre warns:

WARNING: Once Perl sees that you need one of $&, "$`", or "$'" anywhere in the program, it has to provide them for every pattern match. This may substantially slow your program. Perl uses the same mechanism to produce $1, $2, etc, so you also pay a price for each pattern that contains capturing parentheses. (To avoid this cost while retaining the grouping behaviour, use the extended regular expression "(?: ... )" instead.) But if you never use $&, "$`" or "$'", then patterns without capturing parentheses will not be penalized. So avoid $&, "$'", and "$`" if you can, but if you can't (and some algorithms really appreciate them), once you've used them once, use them at will, because you've already paid the price. As of 5.005, $& is not so costly as the other two.

So avoid using $& and friends. A simple workaround is to put parens around the whole regex, and use $1 instead.

Depending on your perl version, perlre might offer other workarounds.

Replies are listed 'Best First'.
Re^2: tight loop regex optimization
by superawesome (Initiate) on Nov 02, 2011 at 04:50 UTC

    I had seen this warning, but was mis-reading it as indicating that the line "use strict" was actually *causing* it, which I just could not figure out. Once the proper lines were pointed out, the fix was exactly what you mentioned (parens and $1).

    Unfortunately this didn't really help performance much in this particular case... but it's a good fix anyway. Thanks!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://935046]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2017-02-20 09:25 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (294 votes). Check out past polls.