I can't figure out how to store the result from cell magic - %%timeit
? I've read:
- Can you capture the output of ipython's magic methods?
- Capture the result of an IPython magic function
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: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.However you still need the
-o
because otherwise the_
variable containsNone
.