Given a pattern p and a string s, suppose p is in lower case. Which of the following two is more efficient?
r = re.compile(r'p', RE.IGNORECASE)
r.match(s)
… or …
r = re.compile(r'p')
r.match(s.lower())
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s really going to depend on the language and engine.
s.lower()andre.IGNORECASEare generally only slow because they’re trying to deal with localization or Unicode strings (see this question). If the regex package you’re using deals with that, and thes.lower()method doesn’t, then thes.lower()method is a clear win. And vice-versa.In general, I’d expect the
s.lower()method to be faster (it tends to be more optimized than regex matching). But in the example as given …… is going to be faster than either of them.