[Tutor] Amazing power of Regular Expressions...

John Fouhy john at fouhy.net
Tue Nov 7 02:28:11 CET 2006


On 07/11/06, Jonathon Sisson <sisson.j at gmail.com> wrote:
> 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?

Nope.

Here's the command again:

python -m timeit -s 'import re' -s 'r = re.compile("[0-9A-Za-z_.-]")'
'r.match("J")'

-s means "startup code".  So, 'import re' and 're.compile(...)' happen
only once, before the main test starts.

You can read help on timeit by typing 'import timeit; help(timeit)' in
the python console.

-- 
John.


More information about the Tutor mailing list