【python】性能测试工具和函数和质量控制
创始人
2024-05-09 05:15:37
0

文章目录

  • 前言
  • 1.timeit 模块
  • 2. cProfile 和 Profile
  • 3.质量控制
  • 总结

前言

一些Python用户对了解同一问题的不同方法的相对性能产生了浓厚的兴趣。 Python提供了一种可以立即回答这些问题的测量工具。


1.timeit 模块

from timeit import Timer
Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
0.020554084330797195
Timer('a,b = b,a', 'a=1; b=2').timeit()
0.01771862991154194

提供两种方式:
1.代码方式
2.命令行方式

也可以自己实现,封装在装饰器中使用,打上注解@timethis就可以随便用了。

from functools import wraps
import timedef timethis(func):"""用于统计业务层各个函数花费多少时间:param func::return:"""@wraps(func)def wrapper(*args, **kwargs):start = time.time()r = func(*args, **kwargs)end = time.time()print(func.__module__, func.__name__, end - start)return rreturn wrapper@timethis
def func1(a:int, b:int)->int:time.sleep(1)return a+bfunc1(10,20)

输出:

__main__ func1 1.0010700225830078

ref:https://docs.python.org/zh-cn/dev/library/timeit.html#module-timeit

2. cProfile 和 Profile

cProfile 和 profile 提供了 Python 程序的 确定性性能分析 。 profile 是一组统计数据,描述程序的各个部分执行的频率和时间。这些统计数据可以通过 pstats 模块格式化为报表。

Python 标准库提供了同一分析接口的两种不同实现:

对于大多数用户,建议使用 cProfile ;这是一个 C 扩展插件,因为其合理的运行开销,所以适合于分析长时间运行的程序。该插件基于 lsprof ,由 Brett Rosen 和 Ted Chaotter 贡献。

profile 是一个纯 Python 模块(cProfile 就是模拟其接口的 C 语言实现),但它会显著增加配置程序的开销。如果你正在尝试以某种方式扩展分析器,则使用此模块可能会更容易完成任务。该模块最初由 Jim Roskind 设计和编写。

import cProfile
import re
cProfile.run('re.compile("foo|bar")')

结果:

214 function calls (207 primitive calls) in 0.002 secondsOrdered by: cumulative timencalls  tottime  percall  cumtime  percall filename:lineno(function)1    0.000    0.000    0.002    0.002 {built-in method builtins.exec}1    0.000    0.000    0.001    0.001 :1()1    0.000    0.000    0.001    0.001 __init__.py:250(compile)1    0.000    0.000    0.001    0.001 __init__.py:289(_compile)1    0.000    0.000    0.000    0.000 _compiler.py:759(compile)1    0.000    0.000    0.000    0.000 _parser.py:937(parse)1    0.000    0.000    0.000    0.000 _compiler.py:598(_code)1    0.000    0.000    0.000    0.000 _parser.py:435(_parse_sub)

命令行方式:

python -m cProfile [-o output_file] [-s sort_order] (-m module | myscript.py)

还有一个是stats, profile和stats都在下面这个ref
ref:https://docs.python.org/zh-cn/dev/library/profile.html#module-profile

print("----------------------------------------------------------------------------------------")import cProfile, pstats, io
from pstats import SortKey
pr = cProfile.Profile()
pr.enable()
for i in range(10):time.sleep(1)
pr.disable()
s = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())
  11 function calls in 10.010 secondsOrdered by: cumulative timencalls  tottime  percall  cumtime  percall filename:lineno(function)10   10.010    1.001   10.010    1.001 {built-in method time.sleep}1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
import cProfilewith cProfile.Profile() as pr:# ... do something ...pr.print_stats()

3.质量控制

开发高质量软件的一种方法是在开发过程中为每个函数编写测试,并在开发过程中经常运行这些测试。

doctest 模块提供了一个工具,用于扫描模块并验证程序文档字符串中嵌入的测试。测试构造就像将典型调用及其结果剪切并粘贴到文档字符串一样简单。这通过向用户提供示例来改进文档,并且它允许doctest模块确保代码保持对文档的真实:

def average(values):"""Computes the arithmetic mean of a list of numbers.>>> print(average([20, 30, 70]))40.0"""return sum(values) / len(values)import doctest
doctest.testmod()   # automatically validate the embedded tests

import unittest

class TestStatisticalFunctions(unittest.TestCase):

def test_average(self):self.assertEqual(average([20, 30, 70]), 40.0)self.assertEqual(round(average([1, 5, 7]), 1), 4.3)with self.assertRaises(ZeroDivisionError):average([])with self.assertRaises(TypeError):average(20, 30, 70)

unittest.main() # Calling from the command line invokes all tests

unittest 模块不像 doctest 模块那样易于使用,但它允许在一个单独的文件中维护更全面的测试集:

总结

与 timeit 的精细粒度级别相反, profile 和 pstats 模块提供了用于在较大的代码块中识别时间关键部分的工具。
手册上就有很好的性能测试工具,profile,stats,都是linux的命令,查看函数调用、耗时的命令有很多,这方便我也在学习,等有了成果在写blog

相关内容

热门资讯

股价走高触发强赎 7月将有两只... 近日,银行正股股价表现强势,多只银行转债触发强制赎回条款。7月1日是杭银转债最后一个交易日,其最后转...
300548,“改名”,股价历... 科技股和顺周期板块再现“跷跷板”走势。今天上午,顺周期板块走强,银行、有色金属、白酒、新能源等板块上...
明阳电路在昆山投资成立集成电路... 人民财讯7月2日电,企查查APP显示,近日,昆山华芯微测集成电路有限公司成立,法定代表人为窦旭才,经...
民生银行“易创E贷”获“202... 在近日举行的第七届数字普惠金融大会上,民生银行“易创E贷”产品凭借在服务科技型中小微企业的创新与实践...
机器人大军逼近,很快,亚马逊的...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! 亚马逊正迅速接近仓库...
广东省教育考试院通报:不存在中... 7月2日,广东省教育考试院发布通报称,7月1日下午广东省初中学业水平考试(以下简称“中考”)数学科目...
国际油价承压 中东油企拟放缓全... 财联社7月2日讯(编辑 秦嘉禾)在国际油价下行压力加剧背景下,中东两大国有能源巨头——沙特阿美(Sa...
光伏50ETF、光伏龙头ETF... 光伏设备板块走强,亿晶光电、欧晶科技涨停。银华光伏50ETF、汇添富光伏龙头ETF、浦银安盛光伏龙头...
世界银行发布重磅预测:黄金今年... 错过上半年,别再错过下半年!世界银行继续看好贵金属前景,黄金、白银、铂金或将继续延续强势……世界银行...
洪灝称高分红投资策略应继续有所... 【#洪灝称高分红投资策略应继续有所表现#】中国股市近期一个显著趋势是分红和回购活动显著增加。其中,许...