Python: What is the cost of re-importing modules?

I am often tempted to import modules in narrow contexts where they are needed. For example, in the body of a function that uses a module. In this case, the import statement can be executed many times.

Besides stylistic issues, what is the cost of doing this?

+4
source share
3 answers

Take a look at the explanation on this site:

https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Import_Statement_Overhead

, Python , , . , , .

+4

import, Python ( import), reload. ( ) import .

; import . , , .

+3

, :

, , , , 20 , , .. , .

, ipython python 2.7.3, , 450 nano :

%%timeit import numpy
pass

100000000 , 3: 11,3

%%timeit import numpy
import numpy
pass

1000000 , 3: 465

+1

All Articles