On Jul 23, Tom Allison said:
Jeff 'japhy' Pinyan wrote:
The /o modifier tells the internal regex compiler that, after this regex has
been compiled 'o'nce, it is never to be compiled again. "Well, what good is
that?" you ask. For your average regex, there is absolutely no difference,
no change in performance: /foo/ and /foo/o are identical. The place where
the /o modifier matters is when there are variables inside the regex, e.g.
/^$field: (.*)/. The /o modifier says that, after the regex has been
compiled for the first time -- which means that all the variables in it have
been interpolated -- *that* compiled regex will take the place of the regex
with the variables in it. Any changes to your variables will be ignored,
for the rest of the program. There's no way to reverse the effect of /o.
If you are running regex without variables, like in this case, it does improve
things if you are doing this through a loop within your code. Otherwise they
are identical.
I disagree. I see no consistent data that shows /rx/o is faster than
/rx/, or vice versa (and I just ran a few benchmarks). Any difference in
speed is purely artificial.
The reason that I am confident of this is because I know how the regex
compiler works internally with Perl. When Perl is compiling your program,
when it sees a constant regex (a regex with no variables in it), it does
something special that it doesn't do when the regex does have variables in
it.
A regex with variables in it goes through three primary steps when
encountered in the running of your code:
1. interpolate all the variables in the regex
2. compare THIS version of the regex with the last version of the regex
we saw at this op-code
2a. if they are the same, use the already compiled version of the
regex (from last time)
2b. if they are different, recompile the regex
3. match the target string using this compiled regex
A regex without variables in it at all goes through a similar set of
steps, but these happen at *compile*-time, not *run*-time:
1. compile this regex
2. create an op-code to match against this compiled regex
Now, when you place an /o modifier on a regex, all that means is that WHEN
THAT REGEX GETS COMPILED, it replaces the op-code it's at with an op-code
that says "match against this compiled regex". That means a regex with
variables and the /o modifier only gets interpolated once, and from then
on, Perl doesn't even recognize it as a regex with variables, but instead
sees it as just another constant regex. Placing an /o modifier on
a constant regex doesn't produce any meaningful loss or gain.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>