in reply to Re^3: speeding up a regex
in thread speeding up a regex
In modern Perls -- I'm not sure which versions qualify here, maybe 5.6+ -- Perl will check whether the contents of the variable has changed. If the content of the variable has not changed, the regexp is not recompiled.Hi ikegami,
...foreach my $word (@words) { if (/\Q$word/) { # BAD!! $word always changes.
Thanks for the very informative post! A quick question: I thought that in a loop like the one above, the iterator variable ($word) was temporarily aliased to each value of the array. So I would expect that as long as the array's contents didn't change, perl would know not to recompile the RE, and so both of your above examples would be the same speed.
But a quick Benchmark agrees with you: putting the RE in the outer loop is about twice as fast as in the inner loop.
Any hints as to what's wrong with my understanding of variable aliasing or RE caching?
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: speeding up a regex
by ikegami (Patriarch) on Jan 03, 2006 at 21:07 UTC |