[英]How to store the result from %%timeit cell magic?
I can't figure out how to store the result from cell magic - %%timeit
? 我无法弄清楚如何存储细胞魔法的结果 -
%%timeit
? I've read: 我读了:
and in this questions answers only about line magic. 而在这个问题中只回答线魔术。 In line mode (
%
) this works: 在线模式(
%
)这适用:
In[1]: res = %timeit -o np.linalg.inv(A)
But in cell mode ( %%
) it does not : 但在单元格模式(
%%
)中它不会 :
In[2]: res = %%timeit -o
A = np.mat('1 2 3; 7 4 9; 5 6 1')
np.linalg.inv(A)
It simply executes the cell, no magic. 它只是执行单元格,没有魔法。 Is it a bug or I'm doing something wrong?
这是一个错误还是我做错了什么?
You can use the _
variable (stores the last result) after the %%timeit -o
cell and assign it to some reusable variable: 您可以在
%%timeit -o
单元格之后使用_
变量(存储最后的结果)并将其分配给某个可重用变量:
In[2]: %%timeit -o
A = np.mat('1 2 3; 7 4 9; 5 6 1')
np.linalg.inv(A)
Out[2]: blabla
<TimeitResult : 1 loop, best of 3: 588 µs per loop>
In[3]: res = _
In[4]: res
Out[4]: <TimeitResult : 1 loop, best of 3: 588 µs per loop>
I don't think it's a bug because cell mode commands must be the first command in that cell so you can't put anything (not even res = ...
) in front of that command. 我不认为这是一个错误,因为单元模式命令必须是该单元格中的第一个命令,因此您不能在该命令前放置任何内容(甚至不是
res = ...
)。
However you still need the -o
because otherwise the _
variable contains None
. 但是,您仍然需要
-o
,否则_
变量包含None
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.