Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Problem with pre-compiled regex

by saranrsm (Acolyte)
on Jan 17, 2012 at 14:07 UTC ( #948323=perlquestion: print w/replies, xml ) Need Help??

saranrsm has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

Problem: Im trying to pre-compile a couple of patterns (Regular expression) and match it into the given text.

Done so far: I have tried doing this without pre-compiled regex, which work perfectly

$text="I have a problem with pre-compiled regex"; @words=qw(have regex); for $a(@words) { while ($text =~ m/$a/g) { print "Found $&\n"; } }

Stuck with: Now when I wanted to pre-compile these patterns (have, regex) and match with $text, I don't get the expected result. I have posted the script which doesn't work, below.

$text="I have a problem with pre-compiled regex"; @words=qw(qr/have/ qr/regex/); for $a(@words) { while ($text =~ /$a/g) { print "Found $&\n"; } }

Replies are listed 'Best First'.
Re: Problem with pre-compiled regex
by moritz (Cardinal) on Jan 17, 2012 at 14:16 UTC
    @words=qw(qr/have/ qr/regex/);

    You probably meant @words = (qr/have/, qr/regex/);. Since qw/ .. / always returns a list of strings, you are trying to match the text against the string qr/have/, not against a precompiled 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

        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.

        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.
        >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

Re: Problem with pre-compiled regex
by Anonymous Monk on Jan 17, 2012 at 15:50 UTC
    When both the Perl language and computer systems were considerably slower than they are today, it made a difference.   Maybe.   But, not anymore and nevermore, I think.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2022-10-04 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (17 votes). Check out past polls.

    Notices?