How to save result from cell magic %% timeit?

I can't figure out how to save the result from cell magic - %%timeit ? I read:

and in this question answers only about line magic. In line mode ( % ), this works:

 In[1]: res = %timeit -o np.linalg.inv(A) 

But in cell mode ( %% ) it is not :

 In[2]: res = %%timeit -o A = np.mat('1 2 3; 7 4 9; 5 6 1') np.linalg.inv(A) 

He just performs a cell, without magic. Is this a mistake or am I doing something wrong?

+5
source share
1 answer

You can use the variable _ (saves the last result) after the cell %%timeit -o and assign it to some reusable variable:

 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 this is a mistake, because the cell mode commands should be the first command in this cell, so you cannot put anything before this command (not even res = ... ).

However, you still need -o , because otherwise the variable _ contains None .

+6
source

Source: https://habr.com/ru/post/1261957/


All Articles