Performance on local constants?
John Machin
sjmachin at lexicon.net
Sat Dec 22 07:04:17 EST 2007
On Dec 22, 9:53 pm, William McBrine <wmcbr... at users.sf.net> wrote:
> Hi all,
>
> I'm pretty new to Python (a little over a month). I was wondering -- is
> something like this:
>
> s = re.compile('whatever')
>
> def t(whatnot):
> return s.search(whatnot)
>
> for i in xrange(1000):
> print t(something[i])
>
> significantly faster than something like this:
>
> def t(whatnot):
> s = re.compile('whatever')
> return s.search(whatnot)
>
> for i in xrange(1000):
> result = t(something[i])
>
> ?
No.
Or is Python clever enough to see that the value of s will be the same
> on every call,
No. It doesn't have a crystal ball.
> and thus only compile it once?
But it is smart enough to maintain a cache, which achieves the desired
result.
Why don't you do some timings?
While you're at it, try this:
def t2(whatnot):
return re.search('whatever', whatnot)
and this:
t3 = re.compile('whatever').search
HTH,
John
More information about the Python-list
mailing list