Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store the result from %%timeit cell magic?

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

  1. Can you capture the output of ipython's magic methods?
  2. 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?

like image 623
godaygo Avatar asked Dec 27 '16 19:12

godaygo


People also ask

How do you use %% Timeit in Jupyter?

The “%timeit” is a line magic command in which the code consists of a single line or should be written in the same line for measuring the execution time. In the “%timeit” command, the particular code is specified after the “%timeit” is separated by a space.

What is %% capture in Python?

IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. from __future__ import print_function import sys. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

What is Run_line_magic?

Third, we use the run_line_magic() method on the ip object to run our line magic. This method takes two arguments: the name of the magic function and the remaining of the arguments for that magic. If you were in an ipython shell, this is the equivalent of running %{magic name} {magic arguments} .

What is the input of a cell magic method?

Magic commands come in two flavors: line magics, which are denoted by a single % prefix and operate on a single line of input, and cell magics, which are denoted by a double %% prefix and operate on multiple lines of input.


2 Answers

You can use the _ variable (stores the last result) after the %%timeit -o cell 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 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 contains None.

like image 140
MSeifert Avatar answered Oct 04 '22 11:10

MSeifert


If you just care about the output of the cell magic, e.g. for recording purposes - and you don't need the extra metadata included in the TimeitResult object, you could also just combine it with %%capture:

%%capture result
%%timeit

A = np.mat('1 2 3; 7 4 9; 5 6 1')
np.linalg.inv(A)

Then you can grab the output from result.stdout, which will yield whatever the output of the cell is - including the timing result.

print(result.stdout)

'26.4 us +- 329 ns per loop (mean +- std. dev. of 7 runs, 10000 loops each)\n'

This works for arbitrary cell magic, and can work as a fallback if the underscore solution isn't working.

like image 20
dnola Avatar answered Oct 04 '22 12:10

dnola