[Python-Dev] local import cost (original) (raw)

Skip Montanaro skip@pobox.com
Wed, 7 May 2003 22:23:58 -0500


Thanks to Raymond H for pointing out the probably fallacy in my original timeit runs. Here's a simple timer which I think gets at what I'm after:

import time
import math
import sys

N = 500000

def fmath():
    import math

def fpass():
    pass

v = sys.version.split()[0]

t = time.clock()
for i in xrange(N):
    fmath()
fmathcps = N/(time.clock()-t)

t = time.clock()
for i in xrange(N):
    fpass()
fpasscps = N/(time.clock()-t)

print "%s fpass/fmath: %.1f" % (v, fpasscps/fmathcps)

On my Mac I get these outputs:

2.1.3 fpass/fmath: 5.0
2.2.2 fpass/fmath: 5.6
2.3b1+ fpass/fmath: 5.3

Naturally, I expect fpass() to run a lot faster than fmath(). If my presumption is correct though, there will be a sharp increase in the ratio, maybe in 2.0 or 2.1, or whenever nested scopes were first introduced.

I can't run anything earlier than 2.1.x (I'll see about building 2.1) on my Mac. I'd have to break out my Linux laptop and do a bunch of downloading and compiling to get earlier results.