I want to store the result of% time, %% time, etc. in an object (variable)

Sometimes you may want to store the output of IPython (Jupyter) line magic, cell magic, and other output in an object (variable) instead of the standard output and process it later. (Example: I want the output, not the return value of the executed function)

It may not be a very complimented method, but I will implement it by temporarily skipping the output destination of sys.stdout to StringIO.

try:
    from io import StringIO  # Python3
except:
    from cStringIO import StringIO  # Python2
import sys

old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
%%time
for i in range(1000000):
    i ** 2
sys.stdout = old_stdout
clock_times = mystdout.getvalue()
print(clock_times)
CPU times: user 467 ms, sys: 0 ns, total: 467 ms
Wall time: 469 ms

Recommended Posts

I want to store the result of% time, %% time, etc. in an object (variable)
I want to leave an arbitrary command in the command history of Shell
I want to grep the execution result of strace
[sh] How to store the command execution result in a variable
How to know the internal structure of an object in Python
[For beginners] I want to explain the number of learning times in an easy-to-understand manner.
I want to know the population of each country in the world.
I made a program in Python that changes the 1-minute data of FX to an arbitrary time frame (1 hour frame, etc.)
Python Note: When you want to know the attributes of an object
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to sort a list in the order of other lists
I want to color a part of an Excel string in Python
Python: I want to measure the processing time of a function neatly
I want to store DB information in list
I want to customize the appearance of zabbix
I want to display the progress in Python!
I tried to display the analysis result of the natural language processing library GiNZA in an easy-to-understand manner
[Python] I want to know the variables in the function when an error occurs!
I want to use Python in the environment of pyenv + pipenv on Windows 10
I want to set a life cycle in the task definition of ECS
I want to see a list of WebDAV files in the Requests module
I want to embed a variable in a Python string
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
I want to increase the security of ssh connections
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
I want to move selenium for the time being [for mac]
An introduction to object orientation-let's change the internal state of an object
I want to use only the normalization process of SudachiPy
I want to judge the authenticity of the elements of numpy array
I want to convert an ISO-8601 character string to Japan time
I want to know the features of Python and pip
I want to make the Dictionary type in the List unique
Keras I want to get the output of any layer !!
I want to align the significant figures in the Numpy array
I want to know the legend of the IT technology world
I want to create a Dockerfile for the time being.
I didn't want to write the AWS key in the program
Get the attributes of an object
The meaning of ".object" in Django
I want to get the name of the function / method being executed
I want to record the execution time and keep a log.
I tried to describe the traffic in real time with WebSocket
I want to manually assign the training parameters of the [Pytorch] model
I compared the calculation time of the moving average written in Python
[Linux] I want to know the date when the user logged in
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
I want to read the html version of "OpenCV-Python Tutorials" OpenCV 3.1 version
How to check the memory size of a variable in Python
LINEbot development, I want to check the operation in the local environment
I tried to make an analysis base of 5 patterns in 3 years
I want to make the second line the column name in pandas
For the time being, I want to convert files with ffmpeg !!
I want to pass the G test in one month Day 1
I want to check the position of my face with OpenCV!
I want to extract an arbitrary URL from the character string of the html source with python
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
I want to pin Spyder to the taskbar
I want to change the color by clicking the scatter point in matplotlib
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I want to output to the console coolly