美文网首页
Python3:代码计时与性能分析

Python3:代码计时与性能分析

作者: ACphart | 来源:发表于2018-08-18 16:12 被阅读29次

1. 使用time模块

from time import time

begin = time()
# your code to time
end = time()
print(end - begin)

2. 在IPython或者Jupyter Notebook中使用%%time魔法命令

In  [1]: %%time
         # your code to run

运行之后会输出这部分代码的运行时间

3. 在IPython或者Jupyter Notebook中使用%%timeit魔法命令

In  [1]: %%timeit
         # your code to run

运行之后会输出这部分代码的运行时间,但和%%time不一样,这个是将这一部分代码重复运行非常多遍然后输出运行时间的平均值。

相关文章

网友评论

      本文标题:Python3:代码计时与性能分析

      本文链接:https://www.haomeiwen.com/subject/mbufiftx.html