基准测试和分析
在本章中,我们将了解基准测试和分析如何帮助解决性能问题。
假设我们编写了一段代码,并且它也给出了所需的结果,但是如果由于需求发生了变化,我们想让这段代码运行得更快一些,该怎么办。在这种情况下,我们需要找出代码的哪些部分正在减慢整个程序的速度。在这种情况下,基准测试和分析会很有用。
什么是基准测试?
基准测试旨在通过与标准进行比较来评估某事物。然而,这里出现的问题是,在软件编程的情况下,基准测试是什么以及我们为什么需要它。对代码进行基准测试意味着代码的执行速度以及瓶颈在哪里。基准测试的一个主要原因是它可以优化代码。
基准测试如何工作?
如果我们谈论基准测试的工作原理,我们需要首先将整个程序作为一个当前状态进行基准测试,然后我们可以组合微基准测试,然后将程序分解为较小的程序。以便找到程序中的瓶颈并对其进行优化。换句话说,我们可以将其理解为将大而难的问题分解为一系列较小且更容易的问题,以便对其进行优化。
用于基准测试的 Python 模块
在 Python 中,我们有一个默认的基准测试模块,称为 timeit。借助 timeit 模块,我们可以测量主程序中一小段 Python 代码的性能。
示例
在下面的 Python 脚本中,我们导入了 timeit 模块,该模块进一步测量执行两个函数(functionA 和 functionB)所需的时间 −
import timeit import time def functionA(): print("Function A starts the execution:") print("Function A completes the execution:") def functionB(): print("Function B starts the execution") print("Function B completes the execution") start_time = timeit.default_timer() functionA() print(timeit.default_timer() - start_time) start_time = timeit.default_timer() functionB() print(timeit.default_timer() - start_time)
运行上述脚本后,我们将获得两个函数的执行时间,如下所示。
输出
Function A starts the execution: Function A completes the execution: 0.0014599495514175942 Function B starts the execution Function B completes the execution 0.0017024724827479076
使用装饰器函数编写我们自己的计时器
在 Python 中,我们可以创建我们自己的计时器,其作用与 timeit 模块一样。这可以借助 装饰器 函数来完成。以下是自定义计时器的示例 −
import random import time def timer_func(func): def function_timer(*args, **kwargs): start = time.time() value = func(*args, **kwargs) end = time.time() runtime = end - start msg = "{func} took {time} seconds to complete its execution." print(msg.format(func = func.__name__,time = runtime)) return value return function_timer @timer_func def Myfunction(): for x in range(5): sleep_time = random.choice(range(1,3)) time.sleep(sleep_time) if __name__ == '__main__': Myfunction()
上面的 Python 脚本有助于导入随机时间模块。我们创建了 timer_func() 装饰器函数。它里面有 function_timer() 函数。现在,嵌套函数将在调用传入的函数之前获取时间。然后它等待函数返回并获取结束时间。这样,我们最终可以让 Python 脚本打印执行时间。脚本将生成如下所示的输出。
输出
Myfunction took 8.000457763671875 seconds to complete its execution.
什么是性能分析?
有时程序员想要测量一些属性,如内存使用、时间复杂度或程序特定指令的使用情况,以测量该程序的实际能力。这种对程序的测量称为性能分析。性能分析使用动态程序分析来进行此类测量。
在后续章节中,我们将了解用于性能分析的不同 Python 模块。
cProfile – 内置模块
cProfile 是一个用于性能分析的 Python 内置模块。该模块是一个具有合理开销的 C 扩展,使其适合对长时间运行的程序进行性能分析。运行后,它会记录所有函数和执行时间。它非常强大,但有时解释和操作有点困难。在下面的例子中,我们在下面的代码上使用 cProfile −
示例
def increment_global(): global x x += 1 def taskofThread(lock): for _ in range(50000): lock.acquire() increment_global() lock.release() def main(): global x x = 0 lock = threading.Lock() t1 = threading.Thread(target=taskofThread, args=(lock,)) t2 = threading.Thread(target= taskofThread, args=(lock,)) t1.start() t2.start() t1.join() t2.join() if __name__ == "__main__": for i in range(5): main() print("x = {1} after Iteration {0}".format(i,x))
上述代码保存在 thread_increment.py 文件中。现在,在命令行中使用 cProfile 执行代码,如下所示 −
(base) D:\ProgramData>python -m cProfile thread_increment.py x = 100000 after Iteration 0 x = 100000 after Iteration 1 x = 100000 after Iteration 2 x = 100000 after Iteration 3 x = 100000 after Iteration 4 3577 function calls (3522 primitive calls) in 1.688 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 5 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:103(release) 5 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:143(__init__) 5 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:147(__enter__) … … … …
从上面的输出中,可以清楚地看出,cProfile 打印出了所有调用的 3577 个函数,以及每个函数所花费的时间和调用次数。以下是我们在输出 − 中获得的列
ncalls − 这是调用的次数。
tottime − 这是给定函数所花费的总时间。
percall − 这是 tottime 除以 ncalls 的商。
cumtime − 这是此函数和所有子函数所花费的累计时间。对于递归函数来说,它甚至是精确的。
percall −它是 cumtime 除以原始调用的商。
filename:lineno(function) − 它基本上提供了每个函数的相应数据。