Python性能分析工具Profile

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

原文地址:https://www.cnblogs.com/-wenli/p/10861526.html

时间: 2024-08-25 18:44:35

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

cProfile——Python性能分析工具

Python自带了几个性能分析的模块:profile.cProfile和hotshot,使用方法基本都差不多,无非模块是纯Python还是用C写的.本文介绍cProfile.  例子 import time def func1(): sum = 0 for i in range(1000000): sum += i def func2(): time.sleep(10) func1() func2() 运行 python -m cProfile del.py 运行结果 结果分析    执行了6个

Android 常用的性能分析工具详解:GPU呈现模式, TraceView, Systrace, HirearchyViewer(转)

此篇将重点介绍几种常用的Android性能分析工具: 一.Logcat 日志 选取Tag=ActivityManager,可以粗略地知道界面Displaying的时间消耗.当我们打开一个Activity的时候,log会打印一串log如下: I/ActivityManager﹕ Displayed xxx.xxx.xxx/TestActivity: +1s272ms (total +3s843ms) 第一个时间表示系统接受到打开的intent到TestActivity界面显示出来的时间1.272秒

三种Linux性能分析工具的比较

无论是在CPU设计.服务器研发还是存储系统开发的过程中,性能总是一个绕不过去的硬指标.很多时候,我们发现系统功能完备,但就是性能不尽如意,这时候就需要找到性能瓶颈.进行优化.首先我们需要结合硬件特点.操作系统和应用程序的特点深入了解系统内部的运行机制.数据流图和关键路径,最好找出核心模块.建立起抽象模型:接着需要利用各种性能分析工具,探测相关模块的热点路径.耗时统计和占比.在这方面,Linux操作系统自带了多种灵活又具有专对性的工具,此外一些厂家也开源了不少优秀的性能分析工具.下面就结合笔者最近

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

测试环境:Ubuntu14.04  on VMWare Kernel:3.13.0-32 系统级性能优化通常包括两个阶段:性能剖析(performance profiling)和代码优化.性能剖析的目标是寻找性能瓶颈,查找引发性能问题的原因及热点代码.代码优化的目标是针对具体性能问题而优化代码或编译选项,以改善软件性能. 在性能剖析阶段,需要借助于现有的profiling工具,如perf等.在代码优化阶段往往需要借助开发者的经验,编写简洁高效的代码,甚至在汇编级别合理使用各种指令,合理安排各种指

PHP性能分析工具XHProf安装使用教程

HProf是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开关来控制是否进行profile.基于浏览 器的性能分析用户界面能更容易查看,或是与同行们分享成果.也能绘制调用关系图.在数据收集阶段,它记录调用次数的追踪和包容性的指标弧在动态callgraph的一个程序. 它独有的数据计算的报告/后处理阶段.在数据收集时,XHProfd通过检测循环来处理递归的函数调用,并通过给递归调用中每个深度的调用一个有用的命名来避开

perf工具-linux下性能分析工具

从2.6.31内核开始,linux内核自带了一个性能分析工具perf,能够进行函数级与指令级的热点查找. perf Performance analysis tools for Linux. Performance counters for Linux are a new kernel-based subsystem that provide a framework for all things performance analysis. It covers hardware level (CP

erlang性能分析工具

Etop 类似top命令,查看erlang进程占用cpu.内存较高的进程 参数: node        atom       erlang node port        integer    The used port accumulate  boolean    If true execution time is accumulated lines       integer    Number of displayed processes interval    integer   

OProfile 性能分析工具

官方网站:http://oprofile.sourceforge.net/news/ oprofile.ko模块本文主要介绍Oprofile工具,适用系统的CPU性能分析,最主要它能深入内核函数,这是很多用户态工具达不到的地方. Oprofile是一个内核态工具,通过oprofile.ko模块内核模块来获取数据需要在加载oprofile.ko模块的时候,传递”timer=1″参数. modprobe oprofile timer=1 oProfilehttp://baike.baidu.com/

Python性能分析指南(未完成)

英文原文:http://www.huyng.com/posts/python-performance-analysis/ 译文:http://www.oschina.net/translate/python-performance-analysis 虽然你所写的每个Python程序并不总是需要严密的性能分析,但是当这样的问题出现时,如果能知道Python生态系统中的许多种工具,这样总是可以让人安心的. 分析一个程序的性能可以归结为回答4个基本的问题: 1.它运行的有多块? 2.那里是速度的瓶颈?