性能分析器cProfile、line_profiler、memory_profiler
2016-11-26 14:21
337 查看
性能分析器主要有两个模块:cProfile、line_profiler、memory_profiler
cProfile是python内置包,它主要用来统计函数调用以及每个函数所占的cpu时间。
line_profiler可以帮你一行一行分析函数性能。
memory_profiler帮你一行一行分析函数内存消耗。
第一步你可以通过cProfile.run()方法或者cProfile.runctx()方法或者cProfile.Profile类来实现
第二步是通过pstats.Stats类实现
一个简单的示例见Instant User’s Manual
使用这个工具有两种方法:
Once you’ve gotten your code setup with the
The
Code Example:
Output
(Time一列1代表1微秒)
这种方法你不需要使用装饰器,也不需要显式使用kernprof(实际上依然是使用这个工具实现的,只不过封装了你看不到)
More Info 见这里
(2) 代码里
首先添加
然后在某个函数上添加装饰器
(3) 命令行执行:
More Info 见这里
Ref
Python性能分析与优化
A guide to analyzing Python performance
Github-rkern/line_profiler
https://pypi.python.org/pypi/memory_profiler
cProfile是python内置包,它主要用来统计函数调用以及每个函数所占的cpu时间。
line_profiler可以帮你一行一行分析函数性能。
memory_profiler帮你一行一行分析函数内存消耗。
1. cProfile
首先你需要运行分析器,生成结果;然后你需要对结果进行各种格式化分析第一步你可以通过cProfile.run()方法或者cProfile.runctx()方法或者cProfile.Profile类来实现
第二步是通过pstats.Stats类实现
一个简单的示例见Instant User’s Manual
2. line_profiler
Once installed you’ll have access to a new module called “line_profiler” as well as an executable script kernprof.py.
使用这个工具有两种方法:
(1)使用命令行
first modify your source code by decorating the function you want to measure with the@profiledecorator.The kernprof.py script automatically injects it into your script’s runtime during execution.
Once you’ve gotten your code setup with the
@profiledecorator, use kernprof.py to run your script.
$ kernprof -l -v fib.py
The
-loption tells
kernprofto inject the
@profiledecorator into your script’s builtins, and
-vtells
kernprofto display timing information once you’re script finishes.如果没有
-v选项,分析结果将会被写入
script_to_profile.py.lprof文件。
(2)使用API(推荐)
第一种方法是通过命令行分析,其实你还可以通过API来分析,line_profiler提供了和
cProfile类似的API,
Code Example:
import line_profiler import sys def test(): print 'haha' prof = line_profiler.LineProfiler(test) prof.enable() # 开始性能分析 test() prof.disable() # 停止性能分析 prof.print_stats(sys.stdout)
Output
haha Timer unit: 5.70172e-07 s Total time: 4.50436e-05 s File: C:/Users/wangjiang/PycharmProjects/Test/sk_test.py Function: test at line 5 Line # Hits Time Per Hit % Time Line Contents -------------------------------------------------------------- 5 def test(): 6 1 79 79.0 100.0 print('haha')
(Time一列1代表1微秒)
这种方法你不需要使用装饰器,也不需要显式使用kernprof(实际上依然是使用这个工具实现的,只不过封装了你看不到)
More Info 见这里
3. memory_profiler
(1) 安装psutil,memory_profilerpip install psutil pip install memory_profiler
(2) 代码里
首先添加
from memory_profiler import profile
然后在某个函数上添加装饰器
@profile
(3) 命令行执行:
python -m memory_profiler example.py
More Info 见这里
Ref
Python性能分析与优化
A guide to analyzing Python performance
Github-rkern/line_profiler
https://pypi.python.org/pypi/memory_profiler
相关文章推荐
- python模块-cProfile和line_profiler(性能分析器)
- Python,django:用line_profiler工具分析代码的性能(一)
- python的性能分析工具line_profiler
- Python的7种性能测试工具:timeit、profile、cProfile、line_profiler、memory_profiler、PyCharm图形化性能测试工具、objgraph
- Oracle PL/SQL 性能分析工具 profiler 说明
- 使用ORM Profiler分析数据访问性能
- [软件分享]-ANTS-Profiler 分析.Net程序性能
- [MySQL优化] -- 如何使用SQL Profiler 性能分析器
- EQATEC Profiler 免费的.net CF 程序性能分析器
- 使用.Net Memory Profiler 分析.Net程序内存泄露
- 用dotTace模仿下老赵的“使用Profiler分析程序性能”
- EQATEC Profiler: 免费的.NETCF代码性能分析工具
- The Miner - 专业Flash AS3性能分析器(profiler)入门教程
- Oracle PL/SQL 性能分析工具 profiler 说明
- 使用Profiler分析程序性能
- 如何使用SQL Profiler 性能分析器
- ANTS Memory Profiler - .NET内存泄漏分析工具
- EQATEC Profiler: 免费的.NETCF代码性能分析工具
- 应用程序性能分析利器 —— Visual Studio Profiler
- Oracle PL/SQL 性能分析工具 profiler 说明