[Tutor] Amazing power of Regular Expressions...

Kent Johnson kent37 at tds.net
Mon Nov 6 17:22:33 CET 2006

Alan Gauld wrote:
> "Michael Sparks" <ms at cerenity.org> wrote
>> That's equivalent to the regular expression:
>>    * ^[0-9A-Za-z_.-]*$
> While using a dictionary is probably overkill, so is a regex.
> A simple string holding all characters and an 'in' test would probably
> be both easier to read and faster. Which kind of illustrates the
> point of the thread I think! :-)
>> Now, which is clearer? If you learn to read & write regular 
>> expressions, then
>> the short regular expression is the clearest form. It's also 
>> quicker.
> Whether its quicker will depend on several factors including the
> implementation of the regex library as well as the length of the 
> string.
> If its a single char I'd expect the dictionary lookup to be faster 
> than
> a regex parse or the string inclusion test... In fact this is how the
> C standard library usually implements functions like toupper()
> and tolower() etc, and for speed reasons.

It's been a long time since I looked at the source for a C standard 
library but I remember these functions being implemented as direct table 
lookup - a static char* indexed by a character value - not dictionary 

If anyone really cares which Python implementation is faster, the timeit 
module is your friend. Assertions like "would probably be faster" or 
"it's also quicker" don't hold much weight. In Python, if you want to 
know what is faster, you must test.

Kent, who doesn't have enough time or interest at the present to try 
timing it himself...

More information about the Tutor mailing list