Python中导入的准确时间 [英] Accurate timing for imports in Python

查看:191
本文介绍了Python中导入的准确时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

timeit 模块非常适合测量小代码片段的执行时间,但是当代码改变全局状态(比如 timeit )时,很难获得准确的时序。

The timeit module is great for measuring the execution time of small code snippets but when the code changes global state (like timeit) it's really hard to get accurate timings.

例如,如果我想花时间导入模块,那么第一次导入将比后续导入花费更长的时间,因为子模块和依赖项已经导入并且文件已经被缓存。因此,使用更大的数字重复,例如:

For example if I want to time it takes to import a module then the first import will take much longer than subsequent imports, because the submodules and dependencies are already imported and the files are already cached. So using a bigger number of repeats, like in:

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

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

不真的很有效,因为一次执行的时间几乎与1000轮相同。我可以执行命令重新加载包:

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

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

但它只比第一个 import 慢10倍似乎表明它也不准确。

But that it's only 10 times slower than the first import seems to suggest it's not accurate either.

完全卸载模块似乎也不可能(卸载模块 Python)。

It also seems impossible to unload a module entirely ("Unload a module in Python").

所以问题是:什么是准确测量 import 时间?

So the question is: What would be an appropriate way to accuratly measure the import time?

推荐答案

由于完全卸载模块几乎是不可能的,因此这个答案背后的灵感可能是这个 ...

Since it's nearly impossible to fully unload a module, maybe the inspiration behind this answer is this...

你可以跑python脚本中的循环运行x次python命令导入 numpy 而另一个无效,并减去+ average:

You could run a loop in a python script to run x times a python command importing numpy and another one doing nothing, and substract 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))

这篇关于Python中导入的准确时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆