问题:python中的%timeit是什么?

我总是像这样阅读代码来计算时间:

%timeit function()

您能在这里解释什么是“%”吗?

我认为,“%”总是用于替换字符串中的某些内容,例如%s表示替换字符串,%d替换数据,但是我不知道这种情况。

I always read the code to calculate the time like this way:

%timeit function()

Can you explain what means “%” here?

I think, the “%” is always used to replace something in a string, like %s means replace a string, %d replace a data, but I have no idea about this case.


回答 0

%timeitipython的魔术函数,可用于计时特定代码段(单个执行语句或单个方法)。

从文档:

%timeit

Time execution of a Python statement or expression

Usage, in line mode:
    %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement

要使用它,例如,如果我们想确定using是否xrange比using更快range,则只需执行以下操作:

In [1]: %timeit for _ in range(1000): True
10000 loops, best of 3: 37.8 µs per loop

In [2]: %timeit for _ in xrange(1000): True
10000 loops, best of 3: 29.6 µs per loop

您将获得他们的时间安排。

主要优点%timeit是:

  • 不必timeit.timeit 从标准库中导入代码,也可以多次运行代码以找出哪种方法更好。

  • %timeit将基于总共2秒的执行窗口自动计算代码所需的运行次数。

  • 您还可以使用当前的控制台变量,而无需传递整个代码片段,以防timeit.timeit构建在另一个可以正常工作的环境中构建的变量。

%timeit is an ipython magic function, which can be used to time a particular piece of code (A single execution statement, or a single method).

From the docs:

%timeit

Time execution of a Python statement or expression

Usage, in line mode:
    %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement

To use it, for example if we want to find out whether using xrange is any faster than using range, you can simply do:

In [1]: %timeit for _ in range(1000): True
10000 loops, best of 3: 37.8 µs per loop

In [2]: %timeit for _ in xrange(1000): True
10000 loops, best of 3: 29.6 µs per loop

And you will get the timings for them.

The major advantage of %timeit are:

  • that you don’t have to import timeit.timeit from the standard library, and run the code multiple times to figure out which is the better approach.

  • %timeit will automatically calculate number of runs required for your code based on a total of 2 seconds execution window.

  • You can also make use of current console variables without passing the whole code snippet as in case of timeit.timeit to built the variable that is built in an another environment that timeit works.


回答 1

这就是所谓的一系魔法中的IPython。它们的独特之处在于其参数仅扩展到当前行的末尾,而魔术本身的确是为命令行开发而构造的。 timeit用于定时执行代码。

如果您想查看所有可以使用的魔法,只需键入:

%lsmagic

以获得线魔术和细胞魔术的列表。

从文档的某些其它神奇信息在这里

IPython具有一个称为魔术的命令系统,该系统有效地提供了一种迷你命令语言,该语言与Python的语法正交,并且可由用户使用新命令进行扩展。魔术应该以交互方式键入,因此它们使用命令行约定,例如使用空格分隔参数,使用破折号表示选项以及命令行环境中的其他典型约定。

根据您是处于模式还是单元格模式,有两种不同的使用方法%timeit。您的问题说明了第一种方式:

In [1]: %timeit range(100)

In [1]: %%timeit 
      : x = range(100)
      :

This is known as a line magic in iPython. They are unique in that their arguments only extend to the end of the current line, and magics themselves are really structured for command line development. timeit is used to time the execution of code.

If you wanted to see all of the magics you can use, you could simply type:

%lsmagic

to get a list of both line magics and cell magics.

Some further magic information from documentation here:

IPython has a system of commands we call magics that provide effectively a mini command language that is orthogonal to the syntax of Python and is extensible by the user with new commands. Magics are meant to be typed interactively, so they use command-line conventions, such as using whitespace for separating arguments, dashes for options and other conventions typical of a command-line environment.

Depending on whether you are in line or cell mode, there are two different ways to use %timeit. Your question illustrates the first way:

In [1]: %timeit range(100)

vs.

In [1]: %%timeit 
      : x = range(100)
      :

回答 2

IPython拦截了这些命令,它们被称为内置魔术命令,这是列表:https : //ipython.org/ipython-doc/dev/interactive/magics.html

您还可以创建自己的自定义魔术,https: //ipython.org/ipython-doc/dev/config/custommagics.html

timeit在这里https://ipython.org/ipython-doc/dev/interactive/magics.html#magic-timeit

IPython intercepts those, they’re called built-in magic commands, here’s the list: https://ipython.org/ipython-doc/dev/interactive/magics.html

You can also create your own custom magics, https://ipython.org/ipython-doc/dev/config/custommagics.html

Your timeit is here https://ipython.org/ipython-doc/dev/interactive/magics.html#magic-timeit


回答 3

我只想添加另一个有用的优点,即使用%timeit通过mu无应答

  • 也可以使用当前的控制台变量,而无需像timeit.timeit那样传递整个代码片段,以构建在timeit可以工作的另一个环境中构建的变量。

PS:我知道这应该是上面回答的评论,但是我目前对此没有足够的声誉,希望我写的内容对某人有帮助,并帮助我赢得足够的声誉,以便下次发表评论。

I would just like to add another useful advantage of using %timeit to answer by mu 無 that:

  • You can also make use of current console variables without passing the whole code snippet as in case of timeit.timeit to built the variable that is built in an another enviroment that timeit works.

PS: I know this should be a comment to answer above but I currently don’t have enough reputation for that, hope what I write will be helpful to someone and help me earn enough reputation to comment next time.


回答 4

我只是想添加一个关于%% timeit的非常微妙的观点。如果它在单元上运行“魔术” ,您将得到错误…

UsageError:%%timeit找不到行魔术函数

…如果%% timeit上方有任何代码/注释行。换句话说,请确保%% timeit是单元格中的第一个命令。

我知道这是所有专家都不会说的一个小问题,但是我只是想为年轻的巫师们以魔术开始增加我的半分钱。

I just wanted to add a very subtle point about %%timeit. Given it runs the “magics” on the cell, you’ll get error…

UsageError: Line magic function %%timeit not found

…if there is any code/comment lines above %%timeit. In other words, ensure that %%timeit is the first command in your cell.

I know it’s a small point all the experts will say duh to, but just wanted to add my half a cent for the young wizards starting out with magic tricks.


回答 5

行魔法前缀与的性格和工作很像OS命令行调用:他们得到作为参数的行,其中参数都没有括号或引号传递的其余部分。单元魔术%%作为双前缀,并且它们是函数,它们不仅作为该行的其余部分作为参数,而且还作为单独的参数作为其下方的行的参数。

Line magics are prefixed with the % character and work much like OS command-line calls: they get as an argument the rest of the line, where arguments are passed without parentheses or quotes. Cell magics are prefixed with a double %%, and they are functions that get as an argument not only the rest of the line, but also the lines below it in a separate argument.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。