Python 中的 Timeit 及其示例?


Python 提供了许多方法来衡量一段 Python 代码的执行时间。一种方法是使用 Python 内置的 `time` 模块,并在程序执行之前和之后保存时间。

Python timeit

当某个程序运行时,许多进程也在后台运行以使该代码可执行。`time` 模块不计算后台进程的执行时间,但是如果您需要精确的时间性能测量,`timeit` 是一个不错的选择。

`timeit` 模块运行代码大约 100 万次(默认值),并考虑运行该代码所需的最短时间。

使用 timeit 获取 Python 执行时间

我们可以通过多种方式使用 `timeit` 模块。最简单的方法之一是直接在 Python CLI 上使用。

示例

我们首先将使用 `timeit` 模块在 Python CLI 上进行操作。当使用 CLI 时,我们会注意到模块本身会决定对同一代码段执行的重复次数。

示例 1

C:\Users\rajesh>python -m timeit "'-'.join(str(n) for n in range(200))"
1000 loops, best of 3: 290 usec per loop

C:\Users\rajesh>python -m timeit "'-'.join(str(n) for n in range(200))"
1000 loops, best of 3: 292 usec per loop

C:\Users\rajesh>python -m timeit "'-'.join(str(n) for n in range(200))"
1000 loops, best of 3: 294 usec per loop

示例 2

接下来,我们将介绍另一个简单的 `timeit` 示例,但首先我们必须使用 `import timeit` 语句导入 `timeit` 模块。如果我们不使用像上面那样的命令行语法,这是必需的。

#Import timeit module
import timeit

# The instructions being timed.
print('x' * 5)
print('x' + 'x' + 'x' + 'x' + 'x')

# Call timeit on the statements and print the time returned.
# ... Specify optional number of iterations.
print(timeit.timeit("y = 'x' * 3", number=10000000))
print(timeit.timeit("xy = 'x' + 'x' + 'x' + 'x' + 'x'", number = 10000000))

我们在上面将语句作为带引号的字符串传递给 `timeit.timeit` 方法,然后通过指定一个数字参数来增加迭代次数。

输出

第一次运行上述程序时,生成的输出

xxxxx
xxxxx
0.9041136896626635
0.7712796073957123

第二次运行上述程序时,生成的输出

xxxxx
xxxxx
0.7317015874427751
0.7312688195585995

第三次运行上述程序时,生成的输出

xxxxx
xxxxx
0.7240862411172824
0.7255863890794246

我们多次(3 次)运行了上述程序,并看到执行时间有所减少。与其手动进行,不如通过程序进行重复。

#Import timeit module
import timeit
# Call timeit on the statements and print the time returned.
# ... Specify optional number of iterations.
print(timeit.repeat("y = 'x' * 3", number=10000000, repeat = 5))
print()
print(timeit.repeat("xy= 'x' + 'x' + 'x' + 'x' + 'x'", number = 10000000, repeat = 5))

输出

[0.7303736343436382, 0.7213687552991258, 0.7362311105941466, 0.7293136666273243, 0.7278277732068212]

[0.7388334197158559, 0.7378481457977326, 0.9486733868277772, 0.735295442480929, 0.7398226849056382]

使用 timeit 模块运行多个语句

我们可以使用 `timeit` 模块使用多个语句。我们用分号分隔每个语句。虽然这不是编写代码的最佳方法,但它有助于指定更长的代码片段。

#Import timeit module
import timeit

# Use semicolon for multiple statements.
print(timeit.repeat("x = 2; x *= 2", number=100000000))
print(timeit.repeat("x = 1; x *= 4", number=100000000))

输出

[24.859605879029118, 23.58795536845994, 23.95826726353284]
[22.70639977603264, 21.380195994245724, 20.71523588130414]

在 Timeit 模块中使用方法和 setup

我们可以通过指定 `setup` 参数在 `timeit` 中使用自定义方法。在这个参数中,我们指定一个导入语句,指示我们调用的方法。

#Import timeit module
import timeit

def func1():
   return 1

def func2():
   return sum([-1, 0, 1, 1])

# Test methods.
print(func1())
print(func2())

# Pass setup argument to call methods.
print(timeit.repeat("func1()", setup="from __main__ import func1"))
print(timeit.repeat("func2()", setup="from __main__ import func2"))

在上面的程序中,我们将 `func1()` 方法与 `func2()` 方法进行基准测试。

输出

1
1
[0.44798489246658874, 0.4411512652046069, 0.44570416580426686]
[1.583622557983199, 1.5712399227517881, 1.5469479030713984]

由于 `func1()` 做的工作较少,因此执行速度更快。

总结

我们在上面看到了如何使用 `timeit` 模块通过 CLI 和脚本测量小段 Python 代码的性能。

更新于:2019年7月30日

浏览量:320

开启您的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.