I want to capture and plot the results from 5 or so timeit
calls with logarithmically increasing sizes of N to show how methodX()
scales with input.
So far I have tried:
output = %timeit -r 10 results = methodX(N)
It does not work...
Can't find info in the docs either. I feel like you should be able to at least intercept the string that is printed. After that I can parse it to extract my info.
Has anyone done this or tried?
PS: this is in an ipython notebook if that makes a diff.
Capturing Output With %%capture 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.
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.
Jupyter Notebook - Big Data Visualization Tool These magic commands are intended to solve common problems in data analysis using Python. In fact, they control the behaviour of IPython itself. Magic commands act as convenient functions where Python syntax is not the most natural one.
IPython - Magic Commands. Magic commands or magic functions are one of the important enhancements that IPython offers compared to the standard Python shell. These magic commands are intended to solve common problems in data analysis using Python. In fact, they control the behaviour of IPython itself.
The %timeit magic runs the given code many times, then returns the speed of the fastest result. The %%timeit cell magic can be used to time blocks of code.
They can in fact make arbitrary modifications to the input they receive, which need not even be a valid Python code at all. They receive the whole block as a single string. To know more about magic functions, the built-in magics and their docstrings, use the magic command.
IPython will run the given command using commands.getoutput (), and return the result formatted as a list (split on ‘n’). Since the output is _returned_, it will be stored in ipython’s regular output cache Out [N] and in the ‘_N’ automatic variables.
This duplicate question Capture the result of an IPython magic function has an answer demonstrating that this has since been implemented.
Calling the %timeit
magic with the -o
option like:
%timeit -o <statement>
returns a TimeitResult
object which is a simple object with all information about the %timeit
run as attributes. For example:
In [1]: result = %timeit -o 1 + 2 Out[1]: 10000000 loops, best of 3: 23.2 ns per loop In [2]: result.best Out[2]: 2.3192405700683594e-08
PS: this is in an ipython notebook if that makes a diff.
No it does not.
On dev there is te %%capture
cell magic. The other way would be to modify the timeit magic to return value instead of printing, or use the timeit module itself. Patches welcomed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With