Precise timing for Python imports

The module is timeit

great for measuring the execution time of small pieces of code, but when the code changes global state (for example timeit

), it is really difficult to get accurate timings.

For example, if I want it to take time to import a module, then the first import will take much longer than subsequent imports, since the submodules and dependencies are already imported and the files are already cached. Therefore, using larger number

repetitions, for example in:

>>> import timeit
>>> timeit.timeit('import numpy', number=1)
0.2819331711316805

>>> # Start a new Python session:
>>> timeit.timeit('import numpy', number=1000)
0.3035142574359181

      

doesn't really work because the time for one execution is almost the same as for 1000 rounds. I can run the command to "reload" the package:

>>> timeit.timeit('imp.reload(numpy)', 'import importlib as imp; import numpy', number=1000)
3.6543283935557156

      

But the fact that it is only 10 times slower than the first import

does not seem to be very accurate.

It is also not possible to unload a module completely ( "Unload a Module in Python" ).

So the question is, what would be a suitable way to measure time accurately import

?

+3


source to share


1 answer


Since it is almost impossible to unload a module completely, perhaps the inspiration behind this answer is this ...

You can run a loop in a python script to run a python command importing x times numpy

, and do nothing else, and subtract as + 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))

      

+2


source







All Articles