网站首页 > 技术文章 正文
这里总结了五个比较好的python性能检测工具,包括内存使用、运行时间、执行次数等方面。
首先,来编写一个基础的python函数用于在后面的各种性能测试。
def base_func():
for n in range(10000):
print('当前n的值是:{}'.format(n))
1、memory_profiler进程监视
memory_profiler是python的非标准库,所以这里采用pip的方式进行安装。 它能够监视进程、了解内存使用等情况。
pip install memory_profiler
复制代码
安装好memory_profiler库以后,直接使用注解的方式进行测试
from memory_profiler import profile
@profile
def base_func1():
for n in range(10000):
print('当前n的值是:{}'.format(n))
base_func1()
# Line # Mem usage Increment Occurrences Line Contents
# =============================================================
# 28 45.3 MiB 45.3 MiB 1 @profile
# 29 def base_func():
# 30 45.3 MiB 0.0 MiB 10001 for n in range(10000):
# 31 45.3 MiB 0.0 MiB 10000 print('当前n的值是:{}'.format(n))
从返回的数据结果来看,执行当前函数使用了45.3 MiB的内存。
2、timeit 时间使用情况
timeit是python的内置模块,可以测试单元格的代码运行时间,由于是内置模块所以并不需要单独安装。
import timeit
def base_func2():
for n in range(10000):
print('当前n的值是:{}'.format(n))
res = timeit.timeit(base_func2,number=5)
print('当前的函数的运行时间是:{}'.format(res))
复制代码
当前的函数的运行时间是:0.9675800999999993
根据上面函数的运行返回结果,函数的运行时间是0.96秒。
3、line_profiler行代码运行时间检测
如果在只需要检测函数的局部运行时间的话就可以使用line_profiler了,它可以检测出每行代码的运行时间。 line_profiler是python的非标准库,使用的使用pip的方式安装一下。
pip install line_profiler
最简便的使用方式直接将需要测试的函数加入即可。
def base_func3():
for n in range(10000):
print('当前n的值是:{}'.format(n))
from line_profiler import LineProfiler
lp = LineProfiler()
lp_wrap = lp(base_func3)
lp_wrap()
lp.print_stats()
# Line # Hits Time Per Hit % Time Line Contents
# ==============================================================
# 72 def base_func3():
# 73 10001 162738.0 16.3 4.8 for n in range(10000):
# 74 10000 3207772.0 320.8 95.2 print('当前n的值是:{}'.format(n))
从运行结果可以看出每行代码的运行时间及比例,注意这里的时间单位是微妙。
4、heartrate可视化检测工具
heartrate最值得推荐的是可以在网页上面向检测心率一样检测程序的执行过程,同时, 他还是非标准库,使用pip的方式进行安装。
# pip install heartrate
import heartrate
heartrate.trace(browser=True)
def base_func4():
for n in range(10000):
print('当前n的值是:{}'.format(n))
运行以后,控制台打印如下日志:
# * Serving Flask app "heartrate.core" (lazy loading)
# * Environment: production
# WARNING: This is a development server. Do not use it in a production deployment.
# Use a production WSGI server instead.
# * Debug mode: off
并且自动打开浏览器地址:http://127.0.0.1:9999

猜你喜欢
- 2024-12-19 提高效率的 10 个 Python 调试技巧
- 2024-12-19 python-IO多路复用(select、poll、epoll)
- 2024-12-19 一篇文章搞懂 Python select 模块
- 2024-12-19 每个python人都离不开的12个python库
- 2024-12-19 使用 Python 和 OpenCV 进行面部识别 - 应用于监控、人脸门禁和考勤等
- 2024-12-19 使用 Python3 uWSGI 实现并发和监控的 Web 应用部署(44)
- 2024-12-19 Python 自动化: eip、cen监控数据对接到 grafana
- 2024-12-19 python 你需要知道的
- 2024-12-19 监控老板一举一动 99行python助你无风险摸鱼
- 2024-12-19 大神教你如何用python监听软件
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)