Python 简明教程

Python - Performance Measurement

给定问题可以通过一个以上的算法解决。因此,我们需要优化解决方案的性能。Python 的 timeit 模块是衡量 Python 应用程序性能的有用工具。

A given problem may be solved by more than one alternative algorithms. Hence, we need to optimize the performance of the solution. Python’s timeit module is a useful tool to measure the performance of a Python application.

这个模块中的 timit() 函数测量 Python 代码的执行时间。

The timit() function in this module measures execution time of your Python code.

Syntax

timeit.timeit(stmt, setup, timer, number)

Parameters

  1. stmt − code snippet for measurement of performance.

  2. setup − setup details arguments to be passed or variables.

  3. timer − uses default timer, so, it may be skipped.

  4. number − the code will be executed this number of times. The default is 1000000.

Example

以下语句使用列表解析来返回一个列表,其中包含范围内的每个数字的2的倍数,范围的上限为100。

The following statement uses list comprehension to return a list of multiple of 2 for each number in the range upto 100.

>>> [n*2 for n in range(100)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34,
36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68,
70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100,
102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126,
128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152,
154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178,
180, 182, 184, 186, 188, 190, 192, 194, 196, 198]

要测量以上语句的执行时间,我们使用如下所示的 timeit() 函数:

To measure the execution time of the above statement, we use the timeit() function as follows −

>>> from timeit import timeit
>>> timeit('[n*2 for n in range(100)]', number=10000)
0.0862189000035869

将执行时间与使用 for 循环追加数字的过程进行比较。

Compare the execution time with the process of appending the numbers using a for loop.

>>> string = '''
... numbers=[]
... for n in range(100):
... numbers.append(n*2)
... '''
>>> timeit(string, number=10000)
0.1010853999905521

结果显示列表解析更高效。

The result shows that list comprehension is more efficient.

声明字符串可以包含一个 Python 函数,其中一个或多个参数 My 可能被传递作为设置代码。

The statement string can contain a Python function to which one or more arguments My be passed as setup code.

我们将找出并比较使用循环的阶乘函数与其递归版本的执行时间。

We shall find and compare the execution time of a factorial function using a loop with that of its recursive version.

使用 for 循环的正常函数为:

The normal function using for loop is −

def fact(x):
   fact = 1
   for i in range(1, x+1):
      fact*=i
   return fact

递归阶乘的定义。

Definition of recursive factorial.

def rfact(x):
   if x==1:
      return 1
   else:
      return x*fact(x-1)

测试这些函数以计算 10 的阶乘。

Test these functions to calculate factorial of 10.

print ("Using loop:",fact(10))
print ("Using Recursion",rfact(10))
Result
Using loop: 3628800
Using Recursion 3628800

现在,我们将使用 timeit() 函数找出各自的执行时间。

Now we shall find their respective execution time with timeit() function.

import timeit

setup1="""
from __main__ import fact
x = 10
"""

setup2="""
from __main__ import rfact
x = 10
"""

print ("Performance of factorial function with loop")
print(timeit.timeit(stmt = "fact(x)", setup=setup1, number=10000))

print ("Performance of factorial function with Recursion")
print(timeit.timeit(stmt = "rfact(x)", setup=setup2, number=10000))

Output

Performance of factorial function with loop
0.00330029999895487
Performance of factorial function with Recursion
0.006506800003990065

递归函数比使用循环的函数慢。

The recursive function is slower than the function with loop.

通过这种方法,我们可以执行 Python 代码的性能测量。

In this way, we can perform performance measurement of Python code.