Python的7种性能测试工具:timeit、profile、cProfile、line_profiler、memory_profiler、PyCharm图形化性能测试工具、objgraph
来源:互联网 发布:什么叫分布式系统 知乎 编辑:程序博客网 时间:2024/05/19 17:58
1.timeit:
>>> import timeit>>> def fun(): for i in range(100000): a = i * i>>> timeit.timeit('fun()', 'from __main__ import fun', number=1)0.02922706632834235>>>
timeit只输出被测试代码的总运行时间,单位为秒,没有详细的统计。
2.profile
profile:纯Python实现的性能测试模块,接口和cProfile一样。
>>> import profile>>> def fun(): for i in range(100000): a = i * i >>> profile.run('fun()') 5 function calls in 0.031 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.016 0.016 :0(exec) 1 0.016 0.016 0.016 0.016 :0(setprofile) 1 0.016 0.016 0.016 0.016 <pyshell#13>:1(fun) 1 0.000 0.000 0.016 0.016 <string>:1(<module>) 1 0.000 0.000 0.031 0.031 profile:0(fun()) 0 0.000 0.000 profile:0(profiler)>>>
ncall:函数运行次数
tottime: 函数的总的运行时间,减去函数中调用子函数的运行时间
第一个percall:percall = tottime / nclall
cumtime:函数及其所有子函数调整的运行时间,也就是函数开始调用到结束的时间。
第二个percall:percall = cumtime / nclall
3.cProfile
profile:c语言实现的性能测试模块,接口和profile一样。
>>> import cProfile>>> def fun(): for i in range(100000): a = i * i >>> cProfile.run('fun()') 4 function calls in 0.024 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.024 0.024 0.024 0.024 <pyshell#17>:1(fun) 1 0.000 0.000 0.024 0.024 <string>:1(<module>) 1 0.000 0.000 0.024 0.024 {built-in method exec} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}>>>ncalls、tottime、percall、cumtime含义同profile。
4.line_profiler
安装:
pip install line_profiler
安装之后kernprof.py会加到环境变量中。
line_profiler可以统计每行代码的执行次数和执行时间等,时间单位为微妙。
测试代码:
C:\Python34\test.py
import time@profiledef fun(): a = 0 b = 0 for i in range(100000): a = a + i * i for i in range(3): b += 1 time.sleep(0.1) return a + bfun()
使用:
1.在需要测试的函数加上@profile装饰,这里我们把测试代码写在C:\Python34\test.py文件上.
2.运行命令行:kernprof -l -v C:\Python34\test.py
输出结果如下:
Total Time:测试代码的总运行时间
Hits:表示每行代码运行的次数
Time:每行代码运行的总时间
Per Hits:每行代码运行一次的时间
% Time:每行代码运行时间的百分比
5.memory_profiler:
memory_profiler工具可以统计每行代码占用的内存大小。
安装:
pip install memory_profilerpip install psutil
测试代码:
同line_profiler。
使用:
1.在需要测试的函数加上@profile装饰2.执行命令: python -m memory_profiler C:\Python34\test.py
输出如下:
6.PyCharm图形化性能测试工具:
PyCharm提供了图像化的性能分析工具,使用方法见利用PyCharm的Profile工具进行Python性能分析。
7.objgraph:
objgraph是一个实用模块,可以列出当前内存中存在的对象,可用于定位内存泄露。
objgraph需要安装:
pip install objgraph
使用方法这里不做描述,自行百度。
- Python的7种性能测试工具:timeit、profile、cProfile、line_profiler、memory_profiler、PyCharm图形化性能测试工具、objgraph
- 性能分析器cProfile、line_profiler、memory_profiler
- android TraceView (图形化性能测试工具)使用入门笔记
- python的性能分析工具line_profiler
- 应用python的性能测量工具cProfile
- 应用python的性能测量工具cProfile
- 使用memory_profiler 与 cProfile ,line_profiler 优化Python程序运行时间和内存消耗实例
- 利用PyCharm的Profile工具进行Python性能分析
- python 性能调试工具(line_profiler)使用
- Python中cProfile性能分析工具的使用
- python性能分析工具:cProfile使用
- 基于python 的性能测试工具
- python 计时工具:timeit
- redis持久化性能测试
- FriedRing(焦圈)--python的开源自动化性能和接口测试框架
- timeit 模块: 测试时间性能
- Protobuf与Avro的序列化性能测试
- 性能测试工具的介绍
- 双卡时候获取指定的imsi码
- [一天几个linux命令] cd
- 交叉编译安装ARM平台上的Qt
- 【前端技巧】html右键禁用和web页面中添加加入qq群的方式
- Java环境搭建一JDK安装下载配置
- Python的7种性能测试工具:timeit、profile、cProfile、line_profiler、memory_profiler、PyCharm图形化性能测试工具、objgraph
- C#在异常中获取HttpStatusCode用法
- 实操VS2015配置内存泄漏检查工具Visual Leak Detector
- 设计模式学习笔记(Java篇)之设计模式原则
- Java 常见内存溢出异常与代码实现
- NodeJS MongoDB CURD 入门
- 子类使用构造函数初始化父类的私有数据
- IDEA生成API接口文档
- Android 项目混淆系列2