Python3:代码计时与性能分析

2018-08-18  本文已影响29人  ACphart

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不一样,这个是将这一部分代码重复运行非常多遍然后输出运行时间的平均值。

上一篇下一篇

猜你喜欢

热点阅读