[Numpy-discussion] slow import of numpy modules
Michael McNeil Forbes
mforbes at physics.ubc.ca
Wed Jul 2 20:00:24 EDT 2008
On 2 Jul 2008, at 3:59 PM, Robert Kern wrote:
> On Wed, Jul 2, 2008 at 17:43, Nathan Jensen
> <Nathan_Jensen at raytheon.com> wrote:
>> Hi,
>>
>> I was wondering if there was any way to speed up the global import of
>> numpy modules. For a simple import numpy, it takes ~250 ms. In
>> comparison, importing Numeric is only taking 40 ms. It appears that
>> even if you only import a numpy submodule, it loads all the
>> libraries,
>> resulting in the painful performance hit. Are there plans to
>> speed up
>> the importing of numpy,
>
> I am not sure how much is possible.
>
>> or at least have it not load libraries that
>> aren't requested?
>
> At this point in time, it is too late to make such sweeping changes
> to the API.
One could use an environmental variable such as
NUMPY_SUPPRESS_TOP_LEVEL_IMPORTS, that, if defined, suppresses the
importing of unneeded packages. This would only affect systems that
define this variable, thus not breaking the API but providing the
flexibility for those that need it. (This or a similar variable
could also contain a list of the numpy components to import
automatically.)
If you want to try this, just modify numpy/__init__.py with something
like the following
import os
fast_import = if 'NUMPY_SUPPRESS_TOL_LEVEL_IMPORTS' in os.environ
del os
if fast_import:
<customised imports>
else:
<standard imports etc.>
del fast_import
Michael.
More information about the NumPy-Discussion
mailing list