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

benjamin.j.hayes at exxonmobil.com benjamin.j.hayes at exxonmobil.com
Thu Nov 20 19:57:27 PST 2008


You could try DProf. It will give you a breakdown of where time is spent in
your program. It may not give you the level of detail you need in this case
as it gives you statistics at the subroutine level, but it might be
enlightening nonetheless, particularly if you use it to compare different
ways of doing things (ie compare your script using $& with a version using
${^MATCH}). There's a useful article here:
http://www.perl.com/pub/a/2004/06/25/profiling.html

Regards

Ben Hayes
Onsite Application Support Coordinator
ExxonMobil Technical Computing Company / Upstream IT
Upstream Technical Computing / UTC Applications / Application & Data
Integration
Esso Australia Pty Ltd
Room 5.59, 12 Riverside Quay, Southbank, VIC 3006, Australia
Phone: +61-3-9270-3538 Fax: +61-3-9270-3600  E-mail:
benjamin.j.hayes at exxonmobil.com
----- Forwarded by Benjamin J Hayes/U-SouthPacific/ExxonMobil on 21/11/2008
02:52 PM -----
                                                                           
             Toby                                                          
             Corkindale                                                    
             <toby.corkinda                                             To 
             le at strategicda           Alfie John <alfiejohn at gmail.com>     
             ta.com.au>                                                 cc 
             Sent by:                 melbourne-pm at pm.org                  
             melbourne-pm-b                                        Subject 
             ounces+benjami           Re: [Melbourne-pm] Just how much     
             n.j.hayes=exxo           does $& slow things down?            
             nmobil.com at pm.                                                
             org                                                           
                                                                           
                                                                           
             21/11/2008                                                    
             02:45 PM                                                      
                                                                           
                                                                           
                                                                           
                                                                           




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.


-Toby

--
Strategic Data Pty Ltd
Ph: 03 9340 9000
_______________________________________________
Melbourne-pm mailing list
Melbourne-pm at pm.org
http://mail.pm.org/mailman/listinfo/melbourne-pm


More information about the Melbourne-pm mailing list