Iterating across a filtered list

Arnaud Delobelle arnodel at googlemail.com
Tue Mar 13 16:19:53 EDT 2007


On Mar 13, 7:36 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> "Arnaud Delobelle" <arno... at googlemail.com> writes:
> > in the for loop.  Moreover you recalculate the regexp for each element
> > of the list.
>
> The re library caches the compiled regexp, I think.

That would surprise me.
How can re.search know that string.lower(search) is the same each
time?  Or else there is something that I misunderstand.

Moreover:

In [49]: from timeit import Timer
In [50]: Timer('for i in range(1000): search("abcdefghijk")', 'import
re; search=re.compile("ijk").search').timeit(100)
Out[50]: 0.36964607238769531

In [51]: Timer('for i in range(1000): re.search("ijk",
"abcdefghijk")', 'import re;
search=re.compile("ijk").search').timeit(100)
Out[51]: 1.4777300357818604

--
Arnaud




More information about the Python-list mailing list