__file__ access extremely slow

Terry Reedy tjreedy at udel.edu
Fri Jun 5 04:53:37 CEST 2009


Zac Burns wrote:
> 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.

Perhaps some of the modules use a delayed import mechanism.
> 
> Python version is 2.5.1
> 
> Code:
> ################################
> for module in sys.modules:
> 	try:
> 		path = module.__file__
> 	except (AttributeError, ImportError):
> 		return

If any modules lack the attribute, you will not scan them all.  Perhaps 
you meant 'continue'?

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




More information about the Python-list mailing list