如何配置我的IPython笔记本,以便始终将执行时间显示为输出的一部分?

Kub*_*uba 4 ipython-notebook

有时我执行一个需要很长时间才能计算的方法

In [1]:

long_running_invocation()

Out[1]:
Run Code Online (Sandbox Code Playgroud)

我常常有兴趣知道花了多少时间,所以我必须写下这个:

In[2]:
    import time

    start = time.time()
    long_running_invocation()
    end = time.time()
    print end - start
Out[2]: 1024
Run Code Online (Sandbox Code Playgroud)

有没有办法配置我的IPython笔记本,以便它自动打印我正在制作的每个调用的执行时间,如下例所示?

In [1]:

long_running_invocation()

Out[1] (1.59s):
Run Code Online (Sandbox Code Playgroud)

ada*_*ari 6

这个ipython扩展可以满足您的需求:https://github.com/cpcloud/ipython-autotime

通过将它放在笔记本的顶部来加载它:

%install_ext https://raw.github.com/cpcloud/ipython-autotime/master/autotime.py %load_ext autotime

加载后,每个后续单元格执行将包括作为其输出的一部分执行所花费的时间.


jga*_*gaw 2

我还没有找到一种方法让每个单元格输出执行代码所需的时间,但是您可以使用单元格魔法来代替您所拥有的时间:%time或%timeit

ipython 细胞魔法