【问题标题】:How to correctly measure the execution time of a cell in jupyter?如何正确测量jupyter中单元格的执行时间?
【发布时间】:2019-09-06 04:31:11
【问题描述】:

我的代码如下所示:

%%time
import time
time.sleep(3)

当我在 jupyter 中执行这个单元格时,我得到了这个输出:

CPU times: user 791 µs, sys: 1.47 ms, total: 2.27 ms
Wall time: 3 s

我的问题是,当我输入sleep(3) 时,总时间不应该是 3 秒而不是 2.27 毫秒。

【问题讨论】:

标签: python time jupyter-notebook jupyter


【解决方案1】:
CPU times: user 791 µs, sys: 1.47 ms, total: 2.27 ms
Wall time: 3 s

CPU times 显示您使用 CPU 的时间。
Wall time 显示自单元格开始以来经过的实时时间。这是你感兴趣的时间。

试试下面的方法看看有什么不同:

%%time
time.sleep(3) #Assuming the time module was already imported

您从未使用过您的 CPU,因此 CPU 时间为 0s

【讨论】:

    【解决方案2】:

    另一种计算执行时间的方法

    您可以使用此代码计算多个单元格或整个代码的总执行时间

    from datetime import datetime 
    
    start_time = datetime.now() 
    
    import time
    time.sleep(3) 
    
    print('Time elapsed (hh:mm:ss.ms) {}'.format(datetime.now() - start_time))
    

    输出:

    Time elapsed (hh:mm:ss.ms) 0:00:03.008139
    

    【讨论】:

      【解决方案3】:

      我建议你使用jupyter扩展Execute Time

      要安装它,请阅读文档或查看该帖子: https://stackoverflow.com/a/50384459/3733974

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-04
        • 1970-01-01
        • 2020-11-28
        • 2011-11-14
        • 1970-01-01
        • 1970-01-01
        • 2019-11-12
        相关资源
        最近更新 更多