Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ???

by mlh2003 (Scribe)
on Feb 18, 2005 at 12:52 UTC ( #432305=note: print w/replies, xml ) Need Help??


in reply to Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ???  

I believe using regex's - even for relatively simple matching - is expensive compared with the simpler (less elaborate) search builtins like index, or even comparison operators. As you know, index is an optimised function specifically created to find a substring within a string. But using a regex fires up the 'regex engine', which in itself is reasonably optimised, but can do a LOT more than the simpler functions and operators. As such it uses more processor resources.

UPDATE Just realized you were comparing it with /foo/ and /bar/, which are still regex's. Well, it is possible that the regex engine can optimize a regex with a constant matching expression (like /foo/) to the point where it is much faster than alternation (/(foo|bar)/). To really be able to answer that question, you'd need to know a fair bit about the internals of the regex engine.

UPDATE 2 See Roy Johnson's reply below. His benchmark verifies my guess :)

  • Comment on Re: Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ???

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2021-10-26 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (90 votes). Check out past polls.

    Notices?