Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Problem with pre-compiled regex

by saranrsm (Acolyte)
on Jan 17, 2012 at 14:42 UTC ( [id://948329]=note: print w/replies, xml ) Need Help??


in reply to Re: Problem with pre-compiled regex
in thread Problem with pre-compiled regex

Dear Monk

Thank you very much for your kind assistance and your tweak made it work perfectly but I have this confusion, the scripts that I posted with and without pre-compiled regex takes almost same time to execute but shouldn't the pre-compiled regex run faster because of pre-compilation...

P.S. I haven't used the @words and $text mentioned, I tried with a larger set of @words and a bigger $text instead of mere (have, regex) and a single sentence for $text as mentioned in the script

Replies are listed 'Best First'.
Re^3: Problem with pre-compiled regex
by moritz (Cardinal) on Jan 17, 2012 at 15:31 UTC

    Compiling a short and simple regex that consists of only one string literal is so fast that you'll probably have trouble to come up with a benchmark to measure it. So if there is a difference, it'll be too small to be easily observable.

    I also think that Perl has an internal cache that avoids recompiling the same regex all over again.

      I also think that Perl has an internal cache that avoids recompiling the same regex all over again.

      The ops that compile regex patterns (qr//, m// and s///) remember the last pattern they compiled.

      _ _ _ | | | | | >perl -Mre=debug -e"for (qw( a a a a b b b a )) { qr/$_/ }" 2>&1 | fin +d "Compiling" Compiling REx "a" Compiling REx "b" Compiling REx "a"

      So if you compile the same pattern many times in a row, you'll get savings.

      It's useful for code that looks like

      for my $pat (...) { for my $string (...) { ... $string =~ /$pat/ ... } }

      But not for code that looks like

      for my $string (...) { for my $pat (...) { ... $string =~ /$pat/ ... } }
Re^3: Problem with pre-compiled regex
by JavaFan (Canon) on Jan 17, 2012 at 16:45 UTC
    shouldn't the pre-compiled regex run faster because of pre-compilation
    1. Both your regexp and your program are fairly trivial. Even if there wasn't any caching involved for non-precompiled regexes, I doubt you'd notice the difference. Whether that's the case for your real $text, I do not know.
    2. Perl has been caching compiled patterns since the 20th century. (5.004 or 5.005, IIRC). This is why /o is usually pointless (if not broken).
    3. Often, the compilation time is dwarved by the runtime anyway.
    4. You'd have to be careful -- if you interpolate your compiled pattern into a larger one, you're paying the price of compilation twice.
Re^3: Problem with pre-compiled regex
by LanX (Saint) on Jan 17, 2012 at 15:14 UTC
    >but shouldn't the pre-compiled regex run faster

    Not necessarily From perlop

    Since Perl may compile the pattern at the moment of execution of qr() operator, using qr() may have speed advantages in some situations,...

    And not any more as often as it used to be!

    ...Perl has many other internal optimizations, ...

    (emphasizes added)

    UPDATE: Furthermore in both your code examples the regexes are just compiled and used once.

    Cheers Rolf

      Actually, the regexes are used twice eacg - they first both match, then fail.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://948329]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-26 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found