Re: Speed comparison of regex versus index, lc, and / /i
- From: Abigail <abigail@xxxxxxxxxx>
- Date: 31 May 2008 11:59:21 GMT
_
Ben Morrow (ben@xxxxxxxxxxxx) wrote on VCCCLXXXVII September MCMXCIII in
<URL:news:mas6h5-nt11.ln1@xxxxxxxxxxxxxxxxxxxxxxx>:
""
"" Quoth "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>:
"" >
"" > Changing this to
"" >
"" > while ( $text =~ /\Q$ss\E/og ) {
"" >
"" > makes regex_find faster by about 1%.
""
"" my $rx = qr/\Q$ss/;
""
"" ...
""
"" while ($text =~ /$rx/g) {
""
"" is both clearer and safer. If the program is modified so that $ss is
"" actually variable (say, the whole thing is made into a sub) then /o
"" would cause it to fail in ways that are rather hard to diagnose.
""
"" Note that m// will only use the precompiled form of the qr// if the
"" $rx is the only thing in the match. Something like /^$rx/ or
"" /$rx1|$rx2/ or even / $rx/x will cause the regex to be recompiled
"" every time all over again.
That hasn't been the case for over a decade or so:
perl -Mre=debug -wE '$re = qr /foo/;
for (qw [bar baz]) {/ $re/}' 2>&1| grep '^Compiling'
Compiling REx "foo"
Compiling REx " (?-xism:foo)"
It only compiles twice, once for the qr //, and once for the m //.
It used to be that way back (before we had qr//), Perl would recompile
a regexp like / $re/, in which case /o was useful.
Nowadays the slight improvement of using /o (there's a sligh improvement
in the sense that when /o is used, Perl doesn't have to check whether
the variables interpolated have changed), IMO, doesn't weight up against
the risk of introducing hard to find errors.
Out of obfuscated code, I would never use /o.
Abigail
--
use lib sub {($\) = split /\./ => pop; print $"};
eval "use Just" || eval "use another" || eval "use Perl" || eval "use Hacker";
.
- Follow-Ups:
- Re: Speed comparison of regex versus index, lc, and / /i
- From: Ben Morrow
- Re: Speed comparison of regex versus index, lc, and / /i
- From: nolo contendere
- Re: Speed comparison of regex versus index, lc, and / /i
- References:
- Speed comparison of regex versus index, lc, and / /i
- From: Ben Bullock
- Re: Speed comparison of regex versus index, lc, and / /i
- From: Ben Bullock
- Re: Speed comparison of regex versus index, lc, and / /i
- From: A. Sinan Unur
- Re: Speed comparison of regex versus index, lc, and / /i
- From: A. Sinan Unur
- Re: Speed comparison of regex versus index, lc, and / /i
- From: Ben Morrow
- Speed comparison of regex versus index, lc, and / /i
- Prev by Date: Re: Speed comparison of regex versus index, lc, and / /i
- Next by Date: FAQ 4.73 How do I print out or copy a recursive data structure?
- Previous by thread: Re: Speed comparison of regex versus index, lc, and / /i
- Next by thread: Re: Speed comparison of regex versus index, lc, and / /i
- Index(es):