python中的%timeit是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29280470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:18:43  来源:igfitidea点击:

What is %timeit in python?

pythonipython

提问by xirururu

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.

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

采纳答案by u1860929

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

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

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

%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 xrangeis any faster than using range, you can simply do:

要使用它,例如,如果我们想知道 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

And you will get the timings for them.

你会得到他们的时间。

The major advantage of %timeitare:

的主要优点%timeit是:

  • that you don'thave to import timeit.timeitfrom 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.timeitto built the variable that is built in an another environment that timeit works.

  • 不必timeit.timeit从标准库中导入,并多次运行代码以确定哪种方法更好。

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

  • 您还可以在不传递整个代码片段的情况下使用当前的控制台变量,因为在timeit.timeit构建在 timeit 工作的另一个环境中构建的变量的情况下。

回答by bakkal

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

IPython 拦截这些,它们被称为内置魔法命令,列表如下: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

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

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

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

回答by miradulo

This is known as a line magicin 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. timeitis used to time the execution of code.

这在 iPython 中被称为线条魔术。它们的独特之处在于它们的参数仅扩展到当前行的末尾,并且魔术本身实际上是为命令行开发而构建的。 timeit用于计时代码的执行。

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 magicsthat 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.

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

Depending on whether you are in lineor cellmode, there are two different ways to use %timeit. Your question illustrates the first way:

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

In [1]: %timeit range(100)

vs.

对比

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

回答by Ankit Agrawal

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

我只是想添加使用%timeit到另一个有用的优点回答通过亩无是:

  • You can also make use of current console variableswithout 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.
  • 还可以使用当前的控制台变量,而无需像 timeit.timeit 一样传递整个代码片段来构建在 timeit 工作的另一个环境中构建的变量。

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.

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

回答by Mohammad Salehi

Line magicsare 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 magicsare 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.

行魔法%字符为前缀,其工作方式与 OS 命令行调用非常相似:它们将行的其余部分作为参数获取,其中参数在传递时不带括号或引号。单元格魔法以双%%为前缀,它们是不仅将行的其余部分作为参数,而且在单独的参数中将其下方的行作为参数的函数。