怎么让jupyter计时

427次阅读
没有评论

怎么让jupyter计时

对于计时有两个十分有用的魔法命令:%%time 和 %timeit。如果你有些代码运行地十分缓慢,而你想确定是否问题出在这里,这两个命令将会非常方便。

1、%%time 将会给出cell的代码运行一次所花费的时间。

%%time
import time
for _ in range(1000):
    time.sleep(0.01)# sleep for 0.01 seconds
 
output:
CPU times: user 196 ms, sys: 21.4 ms, total: 217 ms
Wall time: 11.6 s

注:window 下好像只能显示 “Wall time”, Ubuntu16.4可以正常显示,其他系统未进行测试。

2、%time 将会给出当前行的代码运行一次所花费的时间。

import numpy
%time numpy.random.normal(size=1000)
 
output:
Wall time: 1e+03 µs

3、%timeit 使用Python的timeit模块,它将会执行一个语句100,000次(默认情况下),然后给出运行最快3次的平均值。

import numpy
%timeit numpy.random.normal(size=100)
 
output:
12.8 µs ± 1.25 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Python学习网,有大量免费的Jupyter使用教程,欢迎大家学习!

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:python工具2022-12-13发表,共计650字。
新手QQ群:570568346,欢迎进群讨论 Python51学习