Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Testing a string for a range of characters

by dsb (Chaplain)
on Jan 24, 2001 at 20:29 UTC ( [id://54019]=note: print w/replies, xml ) Need Help??


in reply to Testing a string for a range of characters

One of the reasons that a regular expression might be inefficient is that it would compile every time that it was checked. To save the time of doing that you might want to try the 'o' modifier. This modifier will compile the pattern only once. Since you have no variable interpolation you can get away with this. So the regex would look like this:
$test_char =~ /^[A-Z]$/o;
Try using that as the regex and then see how fast it is. - kel -

Replies are listed 'Best First'.
Re: Re: Testing a string for a range of characters
by Fastolfe (Vicar) on Jan 24, 2001 at 20:35 UTC
    This is incorrect. The regex is compiled only once so long as its contents have no interpolated variables. The /o flag does nothing in this case.
      Ummmm. It most certainly does :D. There is no variable interpolation going on this Regex. Not the one I put up there anyway. Point it out if you don't mind. - kel -
        I don't understand.. I was noting that the /o flag is only useful for compiling a regex once, to avoid recompiling it later. However, this is only a factor if the regex has an interpolated variable. In other words, if Perl notices that the regex will be constant between calls, it will just compile it once anyway. The /o flag does nothing in this case. If Perl sees any interpolated variables, it has to assume that this variable may be different between regex tests, so it recompiles it every time, unless the /o option is given.
Re: Re: Testing a string for a range of characters
by BoredByPolitics (Scribe) on Jan 24, 2001 at 20:49 UTC
    Hmm, good suggestion, but these are the results I get -

    Benchmark: timing 10000000 iterations of comp, exist, regex, regexo... comp: 53 wallclock secs (53.96 usr + 0.00 sys = 53.96 CPU) exist: 49 wallclock secs (49.23 usr + 0.00 sys = 49.23 CPU) regex: 78 wallclock secs (77.40 usr + 0.00 sys = 77.40 CPU) regexo: 78 wallclock secs (77.12 usr + 0.00 sys = 77.12 CPU)

    As you can see regexo isn't showing much difference. Could this be because I'm using /^[A..Z]$/o instead of your suggested /^[A-Z]$/o, or are they functionally equivalent?

    Pete

Log In?
Username:
Password:

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

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

    No recent polls found