[Tutor] Amazing power of Regular Expressions...

Jonathon Sisson sisson.j at gmail.com
Mon Nov 6 19:59:21 CET 2006


Just out of curiousity (since I really can't say myself), does the code 
below import re each time it loops?  I ran the same commands and saw 
quite similar results (0.176 usec per loop for the first test and 0.993 
usec per loop for the second test), and I was just curious if that 
import (and the re.compile, for that matter) happen with each loop?

Jonathon

John Fouhy wrote:
> On 07/11/06, Kent Johnson <kent37 at tds.net> wrote:
>> 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.
> 
> Hmm, what exactly is the contentious code?
> 
> Morpork:~ repton$ python -m timeit -s
> 's="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-"'
> '"J" in s'
> 1000000 loops, best of 3: 0.295 usec per loop
> Morpork:~ repton$ python -m timeit -s 'import re' -s 'r =
> re.compile("[0-9A-Za-z_.-]")' 'r.match("J")'
> 1000000 loops, best of 3: 1.16 usec per loop
> 


More information about the Tutor mailing list