笔记-Python-cProfile
笔记-Python-cProfile
1. 简介
python官方提供了cProfile和profile对程序进行性能分析,建议使用cProfile;
cProfile:基于lsprof的用C语言实现的扩展应用,运行开销比较合理,适合分析运行时间较长的程序,推荐使用这个模块;
profile:纯Python实现的性能分析模块,接口和cProfile一致。但在分析程序时增加了很大的运行开销。不过,如果你想扩展profiler的功能,可以通过继承这个模块实现;
引用于python3.6.5官方文档-标准库-27.4
2. cProfile使用
2.1. 模块简介
profile.run(command,filename = None,sort = -1 )
该函数执行并从执行中收集分析统计数据。如果没有给出文件名,则该函数创建一个Stats 实例并打印一个简单的性能分析报告。如果指定了排序方式,此Stats实例以指定方式排序。
常用参数:
# 直接把分析结果打印到控制台
cProfile.run("test()")
# 把分析结果保存到文件中
cProfile.run("test()", filename="result.out")
# 指定排序方式
cProfile.run("test()", filename="result.out", sort="cumulative")
import cProfile
import re
cProfile.run('re.compile("foo|bar")')
结果
126 function calls in 0.000 seconds #126个调用,数字指原始调用,不包括递归;
Ordered by: standard name #
#调用数量,时间开销(不包括子函数),
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
4 0.000 0.000 0.000 0.000 enum.py:265(__call__)
4 0.000 0.000 0.000 0.000 enum.py:515(__new__)
2 0.000 0.000 0.000 0.000 enum.py:801(__and__)
1 0.000 0.000 0.000 0.000 re.py:231(compile)
1 0.000 0.000 0.000 0.000 re.py:286(_compile)
1 0.000 0.000 0.000 0.000
…仅罗列部分
class profile.Profile(timer = None,timeunit = 0.0,subcalls = True,builtins = True )
这个类通常只在需要比cProfile.run()函数提供更精确的分析控制时才使用。
使用案例:
import cProfile, pstats, io
pr = cProfile.Profile()
pr.enable()
# ... do something ...
pr.disable()
s = io.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())
函数释义:
enable()开始收集分析数据。
disable()停止收集分析数据。
create_stats()停止收集性能数据并创建stats对象。
print_stats(sort = -1 )创建一个Stats对象并将结果输出到stdout。
dump_stats(filename)将当前分析的结果写入文件(二进制格式)。
runcall(func, *args, **kwargs): 收集被调用函数func的性能分析数据Stats类
pstats模块提供的Stats类可以帮助我们读取和操作stats文件(二进制格式)
3. 关于Stats类
Stats类源自pstats模块,使用前import pstats
主要用于格式化展示cprofile的分析结果;
Stats类可以接受stats文件名,也可以直接接受cProfile.Profile对象作为数据源。
strip_dirs(): 删除报告中所有函数文件名的路径信息
dump_stats(filename): 把stats中的分析数据写入文件(效果同cProfile.Profile.dump_stats())
sort_stats(*keys): 对报告列表进行排序,函数会依次按照传入的参数排序,关键词包括calls, cumtime等,具体参数参见官方文档;
reverse_order(): 逆反当前的排序
print_stats(*restrictions): 把信息打印到标准输出。*restrictions用于控制打印结果的形式, 例如(10, 1.0, ".*.py.*")表示打印所有py文件的信息的前10行结果。
笔记-Python-cProfile的更多相关文章
- 笔记-python操作mysql
笔记-python操作mysql 1. 开始 1.1. 环境准备-mysql create database db_python; use db_python; create tabl ...
- 笔记-python异常信息输出
笔记-python异常信息输出 1. 异常信息输出 python异常捕获使用try-except-else-finally语句: 在except 语句中可以使用except as e,然后通 ...
- 笔记-python -asynio
笔记-python -asynio 1. 简介 asyncio是做什么的? asyncio is a library to write concurrent code using the a ...
- 笔记-python lib-pymongo
笔记-python lib-pymongo 1. 开始 pymongo是python版的连接库,最新版为3.7.2. 文档地址:https://pypi.org/project/pymong ...
- 笔记-python tutorial-9.classes
笔记-python tutorial-9.classes 1. Classes 1.1. scopes and namespaces namespace: A namespace is ...
- MongoDB学习笔记:Python 操作MongoDB
MongoDB学习笔记:Python 操作MongoDB Pymongo 安装 安装pymongopip install pymongoPyMongo是驱动程序,使python程序能够使用Mong ...
- 机器学习实战笔记(Python实现)-08-线性回归
--------------------------------------------------------------------------------------- 本系列文章为<机器 ...
- 机器学习实战笔记(Python实现)-05-支持向量机(SVM)
--------------------------------------------------------------------------------------- 本系列文章为<机器 ...
- 机器学习实战笔记(Python实现)-04-Logistic回归
--------------------------------------------------------------------------------------- 本系列文章为<机器 ...
- 机器学习实战笔记(Python实现)-03-朴素贝叶斯
--------------------------------------------------------------------------------------- 本系列文章为<机器 ...
随机推荐
- C#中动态创建数据库和数据表,很经典【转】
用ADOX创建access数据库方法很简单,只需要new一个Catalog对象,然后调用它的Create方法就可以了,如下: ADOX.Catalog catalog = new Catalog(); ...
- 会话跟踪之Session
Session是服务端使用记录客户端状态的一种机制,Session使用简单,但是和Cookie相比,增加了服务器的存储压力[因为为了追求速度,服务器将Session放置在了内存中].Cookie是保存 ...
- Sometimes it takes going through something so awful to realize the beauty that is out there in this world.
Sometimes it takes going through something so awful to realize the beauty that is out there in this ...
- ABAP:从例子学习ABAP
1.插入内表行: *插入内表行: DATA: BEGIN OF man, name(20) TYPE c, high TYPE p DECIMALS 2, weight TYPE p DECIMALS ...
- 私有npm下载资源
私有npm库下载资源需要用户名和密码,这个需要创建npm库的人提供. 使用方法: npm login --registry=仓库地址 Username: 用户名 Password: 密码 Email: ...
- C++ 强制类型转换(转载)
转载自:http://www.weixueyuan.net/view/6329.html 在C++语言中新增了四个关键字static_cast.const_cast.reinterpret_cast和 ...
- uvm_dpi——DPI在UVM中的实现(一)
文件: src/dpi/uvm_dpi.svh 类: 无 SystemVerilog DPI,全称SystemVerilog直接编程接口 (英语:SystemVerilog Direct Pro ...
- .net密码找回
using System; using System.Collections.Generic; using System.Text; using System.Net.Mail; using Syst ...
- Android商城开发系列(十一)—— 首页秒杀布局实现
首页秒杀布局如下图: 布局使用的是LinearLayout和RecyclerView去实现,新建seckkill_item.xml,代码如下所示: <?xml version="1.0 ...
- 根据图片的URL来实例化图片
正常的Image图片类实例化的时候都需要使用本地的虚拟路径而不能使用URL,如果使用URL就会出现 不支持 URI 格式 这样的问题,正确的写法如下: HttpWebRequest reques ...