[PYTHON] Process the result of% time,% timeit

It's well known that Jupyter / IPython can measure execution time with % time and% timeit, but have you ever wanted to process or visualize this?

You can use %% capture to treat standard output as a Python object. Let's actually do it.

%%capture result
%timeit 2 ** 100

The argument of %% capture is the object name, please refer to the help for other options.

  %capture [--no-stderr] [--no-stdout] [--no-display] [output]

run the cell, capturing stdout, stderr, and IPython's rich display() calls.

positional arguments:
  output        The name of the variable in which to store output. This is a utils.io.CapturedIO
                object with stdout/err attributes for the text of the captured output.
                CapturedOutput also has a show() method for displaying the output, and __call__ as
                well, so you can use that to quickly display the output. If unspecified, captured
                output is discarded.

If you look at the stdout attribute, you can see that the standard output is stored in a string.

print(result.stdout)
292 ns +- 4.24 ns per loop (mean +- std. dev. of 7 runs, 1000000 loops each)

Let's convert this 292 ns part into seconds so that it can be treated as a numerical value.

value, unit = result.stdout.split()[:2]
float(value) * {"s": 1, "ms": 1e-3, "us": 1e-6, "ns": 1e-9}[unit]
2.92e-07

Not limited to % timeit, you can use %% capture to process various outputs from Jupyter. Have a good Jupyter life!

Recommended Posts

Process the result of% time,% timeit
Scraping the result of "Schedule-kun"
Implement part of the process in C ++
The result of installing python in Anaconda
Set the process name of the Python program
measurement of time
I measured the run queue wait time of a process on Linux
View the result of geometry processing in Python
Read the output of subprocess.Popen in real time
Send Gmail at the end of the process [Python]
Tucker decomposition of the hay process with HOOI
Extract only complete from the result of Trinity
At the time of python update on ubuntu
I want to store the result of% time, %% time, etc. in an object (variable)
The beginning of cif2cell
A memorandum about the warning of the pylint output result
I want to grep the execution result of strace
Automatically determine and process the encoding of the text file
The meaning of self
The process of installing Atom and getting Python running
Kaggle competition process from the perspective of score transitions
the zen of Python
The story of sys.path.append ()
Measurement of execution time
Decrease the class name of the detection result display of object detection
Output the output result of sklearn.metrics.classification_report as a CSV file
Check the processing time and the number of calls for each process in python (cProfile)
Revenge of the Types: Revenge of types
What you should not do in the process of time series data analysis (including reflection)
Understand the process of merge sort. Finely disassemble following the flow.
Measure the execution result of the program in C ++, Java, Python.
Let's execute the command on time with the bot of discord
Grep so that grep does not appear at the time of grep
Summary of stumbling blocks in Django for the first time
I want to use only the normalization process of SudachiPy
One liner that randomly rewrites the startup time of cron
Reformat the timeline of the pandas time series plot with matplotlib
Debug by attaching to the Python process of the SSH destination
The process of making Python code object-oriented and improving it
[Nonparametric Bayes] Estimating the number of clusters using the Dirichlet process
Output the result of gradient descent method as matplotlib animation
Align the version of chromedriver_binary
Kaggle for the first time (kaggle ①)
10. Counting the number of lines
The story of building Zabbix 4.4
Towards the retirement of Python2
[Apache] The story of prefork
Kill the process with sudo kill -9
Put the process to sleep for a certain period of time (seconds) or more in Python
Compare the fonts of jupyter-themes
About the ease of Python
Get the number of digits
Explain the code of Tensorflow_in_ROS
Reuse the results of clustering
Kaguru for the first time
GoPiGo3 of the old man
Calculate the number of changes
Change the theme of Jupyter
The popularity of programming languages
Change the style of matplotlib
Visualize the orbit of Hayabusa2