[Python-Dev] Startup time

Jeff Epler jepler@unpythonic.net
Wed, 7 May 2003 19:53:44 -0500


On Wed, May 07, 2003 at 07:55:03PM -0400, Raymond Hettinger wrote:
> I don't think timeit.py helps here.  It works by substituting *both* 
> the setup and statement inside a compiled function.  
> 
> So, *none* of the above timings show the effect of a top level import 
> versus one that is inside a function.  It does compare 1 deep nesting 
> to 2 levels deep.

This program prints clock() times for 4e6 imports, first at global and then
at function scope.  Function scope wins a little bit, possibly due to the
speed of STORE_FAST instead of STORE_GLOBAL (or would it be STORE_NAME?)

########################################################################
# (on a different machine than my earlier timeit results, running 2.2.2)
# time for global import 30.21
# time for function import 27.31

import time, sys

t0 = time.clock()
for i in range(1e6):
	import sys; import sys; import sys; import sys;
t1 = time.clock()

print "time for global import", t1-t0

def f():
	for i in range(1e6):
		import sys; import sys; import sys; import sys;

t0 = time.clock()
f()
t1 = time.clock()
print "time for function import", t1-t0
########################################################################

If Skip is thinking of a slowdown for import and function scope, could it
be the {LOAD,STORE}_FAST performance killer 'import *'? (wow, LOAD_NAME
isn't as much slower than LOAD_FAST as you might expect..)

########################################################################
# time for <function f at 0x816306c> 27.9
# time for <function g at 0x8159e9c> 37.94

import new, sys, time
m = new.module('m')
sys.modules['m'] = m
m.__dict__.update({'__all__': ['x'], 'x': None})

def f():
        from m import x
	x; x; x; x; x; x; x; x; x; x
	x; x; x; x; x; x; x; x; x; x
	x; x; x; x; x; x; x; x; x; x
	x; x; x; x; x; x; x; x; x; x
	x; x; x; x; x; x; x; x; x; x

def g():
        from m import *
	x; x; x; x; x; x; x; x; x; x
	x; x; x; x; x; x; x; x; x; x
	x; x; x; x; x; x; x; x; x; x
	x; x; x; x; x; x; x; x; x; x
	x; x; x; x; x; x; x; x; x; x

for fn in f, g:
	t0 = time.clock()
	for i in range(1e6): fn()
	t1 = time.clock()
	print "time for", fn, t1-t0
########################################################################