The exact time of import in Python
>Timeit modules are great for measuring the execution time of small pieces of code, but when the code changes global state (such as Timeit
), it is difficult to get accurate timing.
For example, if I want to calculate when a module is imported, the first import will take longer than subsequent imports because the submodules and dependencies have already been imported and the file has been cached. So use larger number
repeats, such as:
>>> import timeit
>>> timeit.timeit('import numpy', number=1)
0.2819331711316805
>>> # Start a new Python session:
>>> timeit.timeit('import numpy', number=1000)
0.3035142574359181
It didn’t really work because one execution took almost the same time as 1000 rounds. I can execute the command to “reload” the package :
>>> timeit.timeit('imp.reload(numpy)', 'import importlib as imp; import numpy', number=1000)
3.6543283935557156
But it’s
only 10 times slower than the first import
, which seems to indicate that it’s also inaccurate.
It also seems impossible to completely uninstall the module ( “Unload a module in Python”).
So the question is: what is the right way to accurately measure import
time?
Solution
Since completely unloading modules is almost impossible, perhaps the inspiration behind this answer came from this….
You can run a loop in your python script to run the python command that imports numpy
x times and another command that does nothing, and then subtract both + average:
import subprocess,time
n=100
python_load_time = 0
numpy_load_time = 0
for i in range(n):
s = time.time()
subprocess.call(["python","-c","import numpy"])
numpy_load_time += time.time()-s
s = time.time()
subprocess.call(["python","-c","pass"])
python_load_time += time.time()-s
print("average numpy load time = {}".format((numpy_load_time-python_load_time)/n))