python性能分析之cProfile模块

cProfile是标准库内建的分析工具的其中一个,另外两个是hotshot和profile

-s cumulative

-s cumulative开关告诉cProfile对每个函数累计花费的时间进行排序,他能让我看到代码最慢的部分。

我们有这样一个函数。

loopdemo.py

def foo():
    for a in range(0, 101):
        for b in range(0, 101):
            if a + b == 100:
                yield a, b
if __name__ == ‘__main__‘:
    for item in foo():
        print(item)

运行下面命令

python3 -m cProfile -s cumulative loopdemo.py

得到如下结果

         206 function calls in 0.001 seconds
         #在0.01秒内共发生了206次函数调用。包括cProfile的开销。

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.001    0.001 {built-in method builtins.exec}
        1    0.000    0.000    0.001    0.001 loopdemo.py:7(<module>)
      102    0.001    0.000    0.001    0.000 loopdemo.py:7(foo)
      101    0.001    0.000    0.001    0.000 {built-in method builtins.print}
        1    0.000    0.000    0.000    0.000 {method ‘disable‘ of ‘_lsprof.Profiler‘ objects}

其中对参数的解释:

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

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

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

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

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

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

需要注意的是cProfile很难搞清楚函数内的每一行发生了什么,是针对整个函数来说的。

-o profile.stats

我们可与你通过这个函数将结果输出到一个文件中,当然文件的后缀名是任意的,这里为了方便后面配合python中使用所以将后缀定为stats。

首先让我们运行下面的命令

python3 -m cProfile -o loopdemo_profile.stats loopdemo.py

然后运行下面的脚本

import pstats
p=pstats.Stats("loopdemo_profile.stats")
p.sort_stats("cumulative")
p.print_stats()
p.print_callers()  # 可以显示函数被哪些函数调用
p.print_callees()  # 可以显示哪个函数调用了哪些函数

可以看到输出了和之前控制台一样的结果


         2006 function calls in 0.005 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.005    0.005 {built-in method builtins.exec}
        1    0.000    0.000    0.005    0.005 loopdemo.py:7(<module>)
     1001    0.004    0.000    0.004    0.000 {built-in method builtins.print}
     1002    0.000    0.000    0.000    0.000 loopdemo.py:30(foo2)
        1    0.000    0.000    0.000    0.000 {method ‘disable‘ of ‘_lsprof.Profiler‘ objects}

   Ordered by: cumulative time

Function                                          was called by...
                                                      ncalls  tottime  cumtime
{built-in method builtins.exec}                   <-
loopdemo.py:7(<module>)                           <-       1    0.000    0.005  {built-in method builtins.exec}
{built-in method builtins.print}                  <-    1001    0.004    0.004  loopdemo.py:7(<module>)
loopdemo.py:30(foo2)                              <-    1002    0.000    0.000  loopdemo.py:7(<module>)
{method ‘disable‘ of ‘_lsprof.Profiler‘ objects}  <- 

   Ordered by: cumulative time

Function                                          called...
                                                      ncalls  tottime  cumtime
{built-in method builtins.exec}                   ->       1    0.000    0.005  loopdemo.py:7(<module>)
loopdemo.py:7(<module>)                           ->    1002    0.000    0.000  loopdemo.py:30(foo2)
                                                        1001    0.004    0.004  {built-in method builtins.print}
{built-in method builtins.print}                  ->
loopdemo.py:30(foo2)                              ->
{method ‘disable‘ of ‘_lsprof.Profiler‘ objects}  ->

line_profiler

安装

pip3 install Cpython
pip3 install Cython git+https://github.com/rkern/line_profiler.git

原文地址:https://www.cnblogs.com/c-x-a/p/10264490.html

时间: 2024-08-01 15:08:19

python性能分析之cProfile模块的相关文章

Python性能分析

Python性能分析 https://www.cnblogs.com/lrysjtu/p/5651816.html https://www.cnblogs.com/cbscan/articles/3341231.html 使用ipdb 使用profile import profile def profileTest(): Total =1; for i in range(10): Total=Total*(i+1) print Total return Total if __name__ ==

Python性能分析工具Profile

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

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

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

Python 性能分析大全

虽然运行速度慢是 Python 与生俱来的特点,大多数时候我们用 Python 就意味着放弃对性能的追求.但是,就算是用纯 Python 完成同一个任务,老手写出来的代码可能会比菜鸟写的代码块几倍,甚至是几十倍(这里不考虑算法的因素,只考虑语言方面的因素).很多时候,我们将自己的代码运行缓慢地原因归结于python本来就很慢,从而心安理得地放弃深入探究. 但是,事实真的是这样吗?面对python代码,你有分析下面这些问题吗: 程序运行的速度如何?         程序运行时间的瓶颈在哪里?   

Python 性能分析入门指南

在岭南六少博客找到的好东西. 注: 本文的原作者是 Huy Nguyen ,原文地址为 A guide to analyzing Python performance 虽然并非你编写的每个 Python 程序都要求一个严格的性能分析,但是让人放心的是,当问题发生的时候,Python 生态圈有各种各样的工具可以处理这类问题. 分析程序的性能可以归结为回答四个基本问题: 正运行的多快 速度瓶颈在哪里 内存使用率是多少 内存泄露在哪里 下面,我们将用一些神奇的工具深入到这些问题的答案中去. 用 tim

Python—— 性能分析入门指南

虽然并非你编写的每个 Python 程序都要求一个严格的性能分析,但是让人放心的是,当问题发生的时候,Python 生态圈有各种各样的工具可以处理这类问题. 分析程序的性能可以归结为回答四个基本问题: 正运行的多快 速度瓶颈在哪里 内存使用率是多少 内存泄露在哪里 下面,我们将用一些神奇的工具深入到这些问题的答案中去. 用 time 粗粒度的计算时间 让我们开始通过使用一个快速和粗暴的方法计算我们的代码:传统的 unix time 工具. 1 2 3 4 $ time python yourpr

Python性能分析指南

http://www.admin10000.com/document/2861.html 尽管并非每个你写的Python程序都需要严格的性能分析,但了解一下Python的生态系统中很多优秀的在你需要做性能分析的时候可以使用的工具仍然是一件值得去做的事. 分析一个程序的性能,最终都归结为回答4个基本的问题: 程序运行速度有多快? 运行速度瓶颈在哪儿? 程序使用了多少内存? 内存泄露发生在哪里? 下面,我们将使用一些优秀的工具深入回答这些问题. 使用time工具粗糙定时 首先,我们可以使用快速然而粗

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个

python性能分析——insert()

我们在list中插入数据时,经常使用这两个函数: append():在列表的末尾增加一个数据 insert():在某个特定位置前加一个数据 Python内的list实现是通过数组实现的,而不是链表的形式,所以每当执行insert()操作时,都要将插入位置的元素向后移动才能在相应的位置插入元素,执行append()操作时,如果分配的空间还足够大的话那么就可以直接插到最后,如果空间不够的话就需要将已有的数据复制到一片更大的空间后再插入新元素,insert()空间不够的话也是同样 所以,在使用inse