Performance on local constants?

William McBrine wmcbrine at users.sf.net
Sat Dec 22 05:53:39 EST 2007


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])

? Or is Python clever enough to see that the value of s will be the same 
on every call, and thus only compile it once?

-- 
09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 -- pass it on



More information about the Python-list mailing list