in reply to Why this code run faster?

First guess, thankfully shot down by chromatic: The o modifier tells Perl to only compile the regex once, so I believe the two seconds you saved were Perl not compiling the same regex ten million times. Recompiling a constant didn't make sense to me so I am glad I was wrong here.

Second: Retesting, it seems that it's /g that speeds things up, not /o. Perhaps someone more familiar with Perl internals can shed some light.

Finally: Ikegami has it.

Replies are listed 'Best First'.
Re^2: Why this code run faster?
by chromatic (Archbishop) on Nov 08, 2007 at 17:14 UTC

    The regex is constant. Why would Perl have to compile it more than once?

Re^2: Why this code run faster?
by diotalevi (Canon) on Nov 08, 2007 at 17:34 UTC

    You are incorrect. /o is only operative when there is interpolation in the pattern. Since there is none - the pattern is compiled only once and at that is at the normal perl compile-time. That is, right along at the same time as the surrounding code. Compilation of interpolated patterns is deferred to runtime. If /o were present then the first interpolated pattern would be baked in.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re^2: Why this code run faster?
by jasonk (Parson) on Nov 08, 2007 at 17:17 UTC

    /o tells the compiler that the variables mentioned in the regex won't change while the program is running, so it only needs to be compiled once. If your regex contains no variables, then /o doesn't do anything.


    We're not surrounded, we're in a target-rich environment!