Python性能分析工具Profile

代码优化的前提是需要了解性能瓶颈在什么地方,程序运行的主要时间是消耗在哪里,对于比较复杂的代码可以借助一些工具来定位,python 内置了丰富的性能分析工具,如 profile,cProfile 与 hotshot 等。其中 Profiler 是 python 自带的一组程序,能够描述程序运行时候的性能,并提供各种统计帮助用户定位程序的性能瓶颈。Python 标准模块提供三种 profilers:cProfile,profile 以及 hotshot。
profile 的使用非常简单,只需要在使用之前进行 import 即可,也可以在命令行中使用。

使用Profile

测试示例:

import profile
def a():
sum = 0
for i in range(1, 10001):
sum += i
return sum def b():
sum = 0
for i in range(1, 100):
sum += a()
return sum
if __name__ == "__main__":
profile.run("b()")

输出结果:

 104 function calls in 0.094 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
1 0.000 0.000 0.094 0.094 :0(exec)
1 0.000 0.000 0.000 0.000 :0(setprofile)
1 0.000 0.000 0.094 0.094 <string>:1(<module>)
1 0.000 0.000 0.094 0.094 profile:0(b())
0 0.000 0.000 profile:0(profiler)
99 0.094 0.001 0.094 0.001 test.py:15(a)
1 0.000 0.000 0.094 0.094 test.py:21(b)

  

其中输出每列的具体解释如下:

●ncalls:表示函数调用的次数;

●tottime:表示指定函数的总的运行时间,除掉函数中调用子函数的运行时间;

●percall:(第一个 percall)等于 tottime/ncalls;

●cumtime:表示该函数及其所有子函数的调用运行的时间,即函数开始调用到返回的时间;

●percall:(第二个 percall)即函数运行一次的平均时间,等于 cumtime/ncalls;

●filename:lineno(function):每个函数调用的具体信息;

如果需要将输出以日志的形式保存,只需要在调用的时候加入另外一个参数。如 profile.run(“profileTest()”,”testprof”)。

  

命令行

如果我们不想在程序中调用profile库使用,可以在命令行使用命令。

import os

def a():
sum = 0
for i in range(1, 10001):
sum += i
return sum def b():
sum = 0
for i in range(1, 100):
sum += a()
return sum print b()

运行命令查看性能分析结果

python -m cProfile test.py

将性能分析结果保存到result文件

python -m cProfile -o result test.py

使用pstats来格式化显示结果

python -c "import pstats; p=pstats.Stats('reslut); p.print_stats()"

python -c "import pstats; p=pstats.Stats('result'); p.sort_stats('time').print_stats()

sort_stats支持一下参数:

calls, cumulative, file, line, module, name, nfl, pcalls, stdname, time

  

测试示例:在代码中直接使用profile与stats

import os
def a():
sum = 0
for i in range(1, 10001):
sum += i
return sum
def b():
sum = 0
for i in range(1, 100):
sum += a()
return sum
print b()
import cProfile
#cProfile.run("b()")
cProfile.run("b()", "result")
import pstats
pstats.Stats('result').sort_stats(-1).print_stats()

refence

https://blog.csdn.net/xiemanR/article/details/69398057

https://www.cnblogs.com/wangjian8888/p/6095772.html

https://blog.csdn.net/kongxx/article/details/52216850

http://ju.outofmemory.cn/entry/46805

Python性能分析工具Profile的更多相关文章

  1. Python 性能分析工具简介

    Table of Contents 1. 性能分析和调优工具简介 1.1. Context Manager 1.2. Decorator 1.3. 系统自带的time命令 1.4. python ti ...

  2. Python性能分析工具

    import cProfile import pstats from flask import Flask,jsonify, request @app.route("/test", ...

  3. Android性能分析工具Profile GPU rendering详细介绍

    如何在一个应用中追踪和定位性能问题,甚至在没有它的源代码的情况下?? “Profile GPU rendering”(GPU渲染分析),一款Android4.1所引入的工具.你可以在“设置”应用的“开 ...

  4. cProfile——Python性能分析工具

    Python自带了几个性能分析的模块:profile.cProfile和hotshot,使用方法基本都差不多,无非模块是纯Python还是用C写的.本文介绍cProfile.  例子 import t ...

  5. Python性能分析

    Python性能分析 https://www.cnblogs.com/lrysjtu/p/5651816.html https://www.cnblogs.com/cbscan/articles/33 ...

  6. 如何进行python性能分析?

    在分析python代码性能瓶颈,但又不想修改源代码的时候,ipython shell以及第三方库提供了很多扩展工具,可以不用在代码里面加上统计性能的装饰器,也能很方便直观的分析代码性能.下面以我自己实 ...

  7. 系统级性能分析工具perf的介绍与使用

    测试环境:Ubuntu16.04(在VMWare虚拟机使用perf top存在无法显示问题) Kernel:3.13.0-32 系统级性能优化通常包括两个阶段:性能剖析(performance pro ...

  8. Python 性能剖分工具

    Python 性能剖分工具 眼看着项目即将完成,却被测试人员告知没有通过性能测试,这种情况在开发中屡见不鲜.接下来的工作就是加班加点地找出性能瓶颈,然后进行优化,再进行性能测试,如此这般周而复始直到通 ...

  9. 系统级性能分析工具perf的介绍与使用[转]

    测试环境:Ubuntu16.04(在VMWare虚拟机使用perf top存在无法显示问题) Kernel:3.13.0-32 系统级性能优化通常包括两个阶段:性能剖析(performance pro ...

随机推荐

  1. 论文阅读:Flow-level State Transition as a New Switch Primitive for SDN

    Name of article:Flow-level State Transition as a New Switch Primitive for SDN Origin of the article: ...

  2. ModelSerializer 使用知识点_serializer.save(project=obj) #外键一定要作为实例传入save函数,否则无法新增成功

    1.有两个模型如下 A.project class Project(models.Model): """ 项目表 """ id = mode ...

  3. linux可用的跨平台C# .net standard2.0 写的高性能socket框架

    能在window(IOCP)/linux(epoll)运行,基于C# .net standard2.0 写的socket框架,可使用于.net Framework/dotnet core程序集,.使用 ...

  4. leetcode-mid-sorting and searching - 56 Merge Intervals

    mycode 出现的问题:比如最后一个元素是[1,10],1小于前面所有元素的最小值,10大于前面所有元素的最大值,而我最开始的思路只考虑了相邻 参考: 思路:如果我只考虑相邻,必须先将list排序, ...

  5. MySQL的explain分析sql语句

    explain分析查询 使用 EXPLAIN 关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的.这可以帮你分析你的查询语句或是表结构的性能瓶颈.通过explain命 ...

  6. oauth2.0协议原理

    OAuth的授权不会使用第三方触及到用户的帐号信息(如用户密码),即第三方无需使用用户的用户名与密码就可以申请获得该用户资源的授权,因此OAuth是安全的. OAuth的作用:就是让“客户端”安全可控 ...

  7. 阶段2 JavaWeb+黑马旅游网_15-Maven基础_第2节 maven的安装和仓库种类_05仓库的种类和彼此关系

    maven工程里面放的是jar包的坐标. 启动项目的时候会根据jar包的坐标到仓库中找对应的坐标 maven的安装目录.conf/settings.xml文件 ${user.home}表示系统盘,用户 ...

  8. PyQt5学习一---环境的安装和配置

    PyQt5环境安装 1.Python环境(我在练习的时候是用的Python3.6.8) 2.PyQt5安装 首先安装sip pip install sip 然后安装PyQt5-tools pip in ...

  9. python启动线程查看线程的名称和id;python杀掉进程的方法

    def cpu_app(): print("CPU") #启动一个线程t=threading.Thread(target=cpu_app,args=()) t.daemon = T ...

  10. cocos2dx基础篇(9) 滑块控件CCControlSlider

    [3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)CCControlEvent 改为强枚举 Control::EventType (4)CCControlEvent ...