我想得到的时间花在单元格执行除了原始的输出从单元格。

为此,我尝试了%%timeit -r1 -n1,但它没有公开在cell中定义的变量。

%%time适用于只包含1条语句的cell。

In[1]: %%time
       1
CPU times: user 4 µs, sys: 0 ns, total: 4 µs
Wall time: 5.96 µs
Out[1]: 1

In[2]: %%time
       # Notice there is no out result in this case.
       x = 1
       x
CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 5.96 µs

最好的方法是什么?

更新

我已经在nbeextension中使用执行时间相当长一段时间了。这是伟大的。

更新2021 - 03

到目前为止,这是正确的答案。从本质上讲,%%time和%%timeit现在都像预期的那样工作。


当前回答

这不是很漂亮,但没有额外的软件

class timeit():
    from datetime import datetime
    def __enter__(self):
        self.tic = self.datetime.now()
    def __exit__(self, *args, **kwargs):
        print('runtime: {}'.format(self.datetime.now() - self.tic))

然后你可以像这样运行它:

with timeit():
    # your code, e.g., 
    print(sum(range(int(1e7))))

% 49999995000000
% runtime: 0:00:00.338492

其他回答

如果你想打印壁单元格的执行时间,这里有一个技巧, 使用

%%time
<--code goes here-->

但这里要确保,%%time是一个神奇的函数, 所以把它放在代码的第一行。

如果你把它放在代码的某行之后它就会给你 使用错误,不能工作。

我只是在单元格的开头添加了%%time,就得到了时间。您可以在Jupyter Spark集群/虚拟环境中使用相同的方法。只需在单元格顶部添加%%time,就会得到输出。在使用Jupyter的星火集群上,我添加到单元格的顶部,我得到如下输出

[1]  %%time
     import pandas as pd
     from pyspark.ml import Pipeline
     from pyspark.ml.classification import LogisticRegression
     import numpy as np
     .... code ....

Output :-

CPU times: user 59.8 s, sys: 4.97 s, total: 1min 4s
Wall time: 1min 18s

当遇到麻烦时,什么意味着什么:

时间还是??时间

详情如下:

Usage, in line mode:
  %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
or in cell mode:
  %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
  code
  code...

Time execution of a Python statement or expression using the timeit
module.  This function can be used both as a line and cell magic:

- In line mode you can time a single-line statement (though multiple
  ones can be chained with using semicolons).

- In cell mode, the statement in the first line is used as setup code
  (executed but not timed) and the body of the cell is timed.  The cell
  body has access to any variables created in the setup code.

我发现克服这个问题的唯一方法是用print执行最后一条语句。

不要忘记单元格魔术以%%开始,行魔术以%开始。

%%time
clf = tree.DecisionTreeRegressor().fit(X_train, y_train)
res = clf.predict(X_test)
print(res)

注意,在单元格内执行的任何更改都不会在下一个单元格中被考虑,当有管道时,这是违反直觉的:

这不是很漂亮,但没有额外的软件

class timeit():
    from datetime import datetime
    def __enter__(self):
        self.tic = self.datetime.now()
    def __exit__(self, *args, **kwargs):
        print('runtime: {}'.format(self.datetime.now() - self.tic))

然后你可以像这样运行它:

with timeit():
    # your code, e.g., 
    print(sum(range(int(1e7))))

% 49999995000000
% runtime: 0:00:00.338492