简体   繁体   中英

How to send the output of %time to a file in Jupyter/IPython Notebook?

The magic command %time produces the time it takes to execute a given code segment (statement) to the output cell in Jupyter Notebook. I would like this to be appended on to a specified file instead.

My objective is to find the runtime of an algorithm in different settings. I am able to automate the parameters of the algorithm, although not able to store the output of the %time (and %timeit ) magic command in a file for further processing.

Is this possible?

Reposting as an answer:

%timeit (but not %time ) has a -o option which makes it return an object, so you can do:

res = %timeit -o foo()

Then you can get the numbers from res and write them to a file however you wish.

For more flexibility, use the timeit module in the standard library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM