[Melbourne-pm] Just how much does $& slow things down?

Toby Corkindale toby.corkindale at strategicdata.com.au
Thu Nov 20 19:45:59 PST 2008

Alfie John wrote:
> Hey Toby,
>     Do you have to use $&, or can you use ${^MATCH} instead? (Which
>     doesn't come with the global performance impact)
> Where does ${^MATCH} come from? I hope you're not talking about $MATCH, 
> which is just english for $&. If it is, you'll still get the huge 
> performance hit.

Nope, definitely ${^MATCH}.
Full text from `perldoc perlre`:

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.

As a workaround for this problem, Perl 5.10.0 introduces
"${^PREMATCH}", "${^MATCH}" and "${^POSTMATCH}", which are equivalent
to "$`", $& and "$'", except that they are only guaranteed to be
defined after a successful match that was executed with the "/p"
(preserve) modifier.  The use of these variables incurs no global
performance penalty, unlike their punctuation char equivalents, however
at the trade-off that you have to tell perl when you want to use them.


Strategic Data Pty Ltd
Ph: 03 9340 9000

More information about the Melbourne-pm mailing list