Re: Speed comparison of regex versus index, lc, and / /i



_
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";
.