Re: Speed comparison of regex versus index, lc, and / /i
- From: Ben Morrow <ben@xxxxxxxxxxxx>
- Date: Sat, 31 May 2008 16:14:13 +0100
Quoth abigail@xxxxxxxxxx:
Ben Morrow (ben@xxxxxxxxxxxx) wrote on VCCCLXXXVII September MCMXCIII in
<URL:news:mas6h5-nt11.ln1@xxxxxxxxxxxxxxxxxxxxxxx>:
""
"" 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 //.
You know, I did actually test that :).
~% perl -Mre=debug -e'$qr=qr/a/; /$qr/' 2>&1 | grep Comp
Compiling REx `a'
~% perl -Mre=debug -e'$qr=qr/a/; / $qr/x' 2>&1 | grep Comp
Compiling REx `a'
Compiling REx ` (?-xism:a)'
If it's actually using the compiled form of the qr//, it doesn't need to
compile for the m// at all. The fact your example only compiles the m//
once is the 'if a variable hasn't changed, don't recompile'
optimization, which applies regardless of qr//:
~% perl -Mre=debug -e'$qr=qr/a/; / $qr/ for 1, 2' 2>&1 | grep Comp
Compiling REx `a'
Compiling REx ` (?-xism:a)'
~% perl -Mre=debug -e'$qr=q/a/; / $qr/ for 1, 2' 2>&1 | grep Comp
Compiling REx ` a'
Out of obfuscated code, I would never use /o.
Me either.
Ben
--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time.
ben@xxxxxxxxxxxx The Levellers, 'Believers'
.
- References:
- 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: Ben Morrow
- Re: Speed comparison of regex versus index, lc, and / /i
- From: Abigail
- 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: Re: Speed comparison of regex versus index, lc, and / /i
- 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):