__file__ access extremely slow

Zac Burns zac256 at gmail.com
Thu Jun 4 21:24:48 EDT 2009


The section of code below, which simply gets the __file__ attribute of
the imported modules, takes more than 1/3 of the total startup time.
Given that many modules are complicated and even have dynamic
population this figure seems very high to me. it would seem very high
if one just considered the time it would take to load the pyc files
off the disk vs... whatever happens when module.__file__ happens.

The calculation appears to be cached though, so a subsequent check
does not take very long.

>From once python starts and loads the main module to after all the
imports occur and this section executes takes 1.3sec. This section
takes 0.5sec. Total module count is ~800.

Python version is 2.5.1

Code:
################################
for module in sys.modules:
	try:
		path = module.__file__
	except (AttributeError, ImportError):
		return
################################



--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games



More information about the Python-list mailing list