简体   繁体   English

如何将%time的输出发送到Jupyter / IPython Notebook中的文件?

[英]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. 魔术命令%time产生了执行给定代码段(语句)到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. 尽管无法将%time (和%timeit )魔术命令的输出存储在文件中以进行进一步处理,但我能够使算法的参数自动化。

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: %timeit (但不是%time )具有-o选项,该选项使它返回对象,因此您可以执行以下操作:

res = %timeit -o foo()

Then you can get the numbers from res and write them to a file however you wish. 然后,您可以从res中获取编号,然后根据需要将其写入文件中。

For more flexibility, use the timeit module in the standard library. 为了获得更大的灵活性,请使用标准库中的timeit模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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