IPython的%timeit魔术的-n和-r参数 [英] -n and -r arguments to IPython's %timeit magic

查看:151
本文介绍了IPython的%timeit魔术的-n和-r参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Jupyter笔记本中使用timeit magic命令为代码块计时.根据文档,timeit带有几个参数.特别是两个控制循环数和重复数.我不清楚这两个论点之间的区别.例如

I would like to time a code block using the timeit magic command in a Jupyter notebook. According to the documentation, timeit takes several arguments. Two in particular control number of loops and number of repetitions. What isn't clear to me is the distinction between these two arguments. For example

import numpy
N = 1000000
v = numpy.arange(N)

%timeit -n 10 -r 500 pass; w = v + v

将运行10个循环和500次重复.我的问题是,

will run 10 loops and 500 repetitions. My question is,

这可以解释为 下列的? (实际计时结果有明显差异)

Can this be interpreted as the following? (with obvious differences the actual timing results)

import time
n = 10
r = 500
T = numpy.empty(r)
for j in range(r):
    t0 = time.time()
    for i in range(n):
        w = v + v
    T[j] = (time.time() - t0)/n

print('Best time is {:.4f} ms'.format(max(T)*1000))

我正在做的一个假设(很可能是不正确的)是,内部循环的时间是通过该循环在n迭代中平均的.然后,执行此循环的500次重复中的最佳操作.

An assumption I am making, and may well be incorrect, is that the time for the inner loop is averaged over the n iterations through this loop. Then the best of 500 repetitions of this loop is taken.

我已经搜索了文档,但没有找到任何确切说明此操作的内容.例如,文档此处

I have searched the documentation, and haven't found anything that specifies exactly what this is doing. For example, the documentation here is

选项:-n:循环执行给定的语句时间.如果未指定该值,则选择一个合适的值.

Options: -n: execute the given statement times in a loop. If this value is not given, a fitting value is chosen.

-r:重复循环迭代时间并获得最佳结果.默认值:3

-r: repeat the loop iteration times and take the best result. Default: 3

关于内部循环的计时方式并没有说什么.最终结果是最佳"结果.什么?

Nothing is really said about how the inner loop is timed. The final results is the "best" of what?

我想计时的代码不涉及任何随机性,因此我想知道是否应该将此内部循环设置为n=1.然后,r重复将解决任何系统可变性.

The code I want to time does not involve any randomness, so I am wondering if I should set this inner loop to n=1. Then, the r repetitions will take care of any system variability.

推荐答案

最新版本的%timeit似乎正在获取r n环平均值的平均值,而不是最佳平均值.

It looks like the latest version of %timeit is taking the average of the r n-loop averages, not the best of the averages.

很明显,这与Python的早期版本有所不同.仍然可以通过TimeResults返回参数获得r平均值的最佳时间,但不再是显示的值.

Evidently, this has changed from earlier versions of Python. The best time of r averages can still be obtained via the TimeResults return argument, but it is no longer the value that is displayed.

评论:我最近从上面运行了这段代码,发现以下语法不再起作用:

Comment : I recently ran this code from above and found that the following syntax no longer works :

n = 1
r = 50
tr = %timeit -n $n -r $r -q -o pass; compute_mean(x,np)

也就是说,不再可能(似乎)使用$var将变量传递给timeit magic命令.这是否意味着该魔术命令应退役并替换为timeit模块?

That is, it is no longer possible (it seems) to use $var to pass a variable to the timeit magic command. Does this mean that this magic command should be retired and replaced with the timeit module?

我正在使用Python 3.7.4.

I am using Python 3.7.4.

这篇关于IPython的%timeit魔术的-n和-r参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆