我想使用 %timeit,但不知道该怎么做

I want to use %timeit, but don't know how to do it

首先,我对 Python 还是很陌生。我看过一些使用 %timeit 的例子,它看起来对做一些优化工作很有用。问题是,我还没有找到如何真正开始使用它。看起来有一些类似的方法可以做到这一点,我也找到了一些 Python 2.7 示例。好的,这里有一些简单的要点供 Python Guru 使用。

我正在使用 Python 3.6.

我需要导入什么?两种导入语法都可以。

import xxxxx    or    from xxxxx import yyyyy

看来我可以在循环和其他一些构造上使用 %timeit。它们也可以用于 "def" 吗?

如果你给我一个 link 并说 "RTFM," 我很好,这至少会让我朝着正确的方向前进。

%timeitipython 魔法;您只能通过 installing IPython and using it as your interactive interpreter instead of plain python. It's built on top of the timeit module's timeit.repeat 使用它(可能重新实现;它做了一些额外的工作以使结果对人类更有用一目了然),您可以在常规 Python 中使用它,但 %timeit的方便需要ipython.