(More) Re: Is __import__ known to be slow in windows?

Joshua Kugler jkugler at bigfoot.com
Fri Nov 30 22:12:10 EST 2007


Ok, so we have this code:

t = timeit.Timer(stmt='r()', setup='from __main__ import r')

sys.path.insert(0,'/path/to/code')

def r():
    for m in ['three','module','names']:
        try:
            x = __import__(m)
        except ImportError, e:
            if not e.message.startswith('No module named'):
                raise
    x = None

Each of those three module names is a directory under /path/to/code with an
empty __init_.py.

On Linux, the run of that code for 1000 iterations takes 0.014 CPU seconds
with 0.007 of that spent in __import__().

On Windows (with on-access checking turned off), 1000 iterations takes 7.9
seconds, with 7.851 seconds of that spent in __import__().

Let's try something...let's modify the code a bit:

sys.path = ['/path/to/code']

def r():
    for m in ['three','module','names']:
        x = __import__(m)
    x = None

cProfile.run('t.timeit(number=%s)' % number, 'stat_file')
p = pstats.Stats('stat_file')
p.sort_stats('time', 'cum').print_stats(.5)

Now, with only my directory in sys.path, the run times are:
Linux: 0.0013 (0.006 spent in __import__)
Windows: 0.0012 (0.007 spent in __import__)

So, it appears walking the directory trees is what is costing the time.

sys.path on Windows:
['', 'C:\\Python25\\lib\\site-packages\\paste-1.5.1-py2.5.egg',
'C:\\WINDOWS\\system32\\python25.zip',
'C:\\Python25\\DLLs',
'C:\\Python25\\lib',
'C:\\Python25\\lib\\plat-win',
'C:\\Python25\\lib\\lib-tk',
'C:\\Python25',
'C:\\Python25\\lib\\site-packages',
'C:\\Python25\\lib\\site-packages\\win32',
'C:\\Python25\\lib\\site-packages\\win32\\lib',
'C:\\Python25\\lib\\site-packages\\Pythonwin']

On Linux:
['', '/usr/lib/python2.5/site-packages/setuptools-0.6c7-py2.5.egg',
'/usr/lib/python2.5/site-packages/simplejson-1.7.3-py2.5-linux-i686.egg',
'/usr/lib/python2.5/site-packages/Paste-1.5.1-py2.5.egg',
'/usr/lib/python2.5/site-packages/TracWebAdmin-0.1.2dev-py2.5.egg',
'/home/jkugler/programming',
'/usr/lib/python25.zip',
'/usr/lib/python2.5',
'/usr/lib/python2.5/plat-linux2',
'/usr/lib/python2.5/lib-tk',
'/usr/lib/python2.5/lib-dynload',
'/usr/local/lib/python2.5/site-packages',
'/usr/lib/python2.5/site-packages',
'/var/lib/python-support/python2.5',
'/usr/lib/site-python']

So, not identical (actually, longer on linux) but similar.

Interesting, at any rate.

j




More information about the Python-list mailing list