• psutil 是因为该包能提升 memory_profiler 的性能


    python 性能分析入门指南

    一点号数据玩家昨天

    限时干货下载:添加微信公众号“数据玩家「fbigdata」”

    回复【7】免费获取【完整数据分析资料!(包括SPSS、SAS、SQL、EXCEL、Project)!】

    英文:yexiaobai

    译文:yexiaobai

    虽然并非你编写的每个 Python 程序都要求一个严格的性能分析,但是让人放心的是,当问题发生的时候,Python 生态圈有各种各样的工具可以处理这类问题。

    用 time 粗粒度的计算时间

    $time pythonyourprogram.py

    real 0m1.028s

    user 0m0.001s

    sys 0m0.003s

    三个输出测量值之间的详细意义在这里 stackoverflow article,但简介在这:

    你会有你的应用程序用完了多少 CPU 周期的即视感,不管系统上其他运行的程序添加的系统和用户时间。

    如果 sys 和 user 时间之和小于 real 时间,然后你可以猜测到大多数程序的性能问题最有可能与 IO wait 相关。

    我们下一步的技术包括直接嵌入代码来获取细粒度的计时信息。下面是我进行时间测量的代码的一个小片段

    import time

    classTimer(object):

    def __init__(selfverbose=False):

    self.verbose=verbose

    def __enter__(self):

    self.start=time.time

    returnself

    def __exit__(self*args):

    self.end=time.time

    self.secs=self.end-self.start

    self.msecs=self.secs *1000 # millisecs

    ifself.verbose:

    为了使用它,使用 Python 的 with 关键字和 Timer 上下文管理器来包装你想计算的代码。当您的代码块开始执行,它将照顾启动计时器,当你的代码块结束的时候,它将停止计时器。

    from timer import Timer

    from redis import Redis

    rdb=Redis

    with Timerast:

    rdb.lpush("foo""bar")

    print"=> elasped lpush: %s s"%t.secs

    rdb.lpop("foo")

    rdb=Redis

    为了看看我的程序的性能随着时间的演化的趋势,我常常记录这些定时器的输出到一个文件中。

    罗伯特克恩有一个不错的项目称为 line_profiler , 我经常使用它来分析我的脚本有多快,以及每行代码执行的频率:

    安装完成后,你将获得一个新模块称为 line_profiler 和 kernprof.py 可执行脚本。

    为了使用这个工具,首先在你想测量的函数上设置 @profile 修饰符。不用担心,为了这个修饰符,你不需要引入任何东西。kernprof.py 脚本会在运行时自动注入你的脚本。

    defprimes(n):

    ifn==2:

    return[2]

    elifn<2:

    return

    s=range(3n+12)

    mroot=n**0.5

    half=(n+1)/2-1

    i=0

    m=3

    whilem<=mroot:

    ifs[i]:

    j=(m*m-3)/2

    s[j]=0

    whilej<half:

    s[j]=0

    j+=m

    i=i+1

    m=2*i+3

    return[2]+[xforxinsifx]

    一旦你得到了你的设置了修饰符 @profile 的代码,使用 kernprof.py 运行这个脚本。

    -l 选项告诉 kernprof 把修饰符 @profile 注入你的脚本,-v 选项告诉 kernprof 一旦你的脚本完成后,展示计时信息。这是一个以上脚本的类似输出:

    Wrote profileresults toprimes.py.lprof

    Timerunit:1e-06s

    File:primes.py

    Function:primes atline2

    Total time:0.00019s

    Line# Hits Time Per Hit % Time Line Contents

    2 @profile

    3 defprimes(n):

    4 1 2 2.0 1.1 ifn==2:

    5return[2]

    6 1 1 1.0 0.5 elifn<2:

    7return

    8 1 4 4.0 2.1 s=range(3n+12)

    9 1 10 10.0 5.3 mroot=n**0.5

    10 1 2 2.0 1.1 half=(n+1)/2-1

    11 1 1 1.0 0.5 i=0

    12 1 1 1.0 0.5 m=3

    13 5 7 1.4 3.7 whilem<=mroot:

    14 4 4 1.0 2.1 ifs[i]:

    15 3 4 1.3 2.1 j=(m*m-3)/2

    16 3 4 1.3 2.1 s[j]=0

    17 31 31 1.0 16.3 whilej<half:

    18 28 28 1.0 14.7 s[j]=0

    19 28 29 1.0 15.3 j+=m

    20 4 4 1.0 2.1 i=i+1

    21 4 4 1.0 2.1 m=2*i+3

    22 50 54 1.1 28.4 return[2]+[xforx 现在我们掌握了很好我们代码的计时信息,让我们继续找出我们的程序使用了多少内存。我们真是非常幸运, Fabian Pedregosa 仿照 Robert Kern 的 line_profiler 实现了一个很好的内存分析器 [memory profiler][5]。

    在这里建议安装 psutil 是因为该包能提升 memory_profiler 的性能。

    想 line_profiler 一样, memory_profiler 要求在你设置 @profile 来修饰你的函数:

    @profile

    def primes(n):

    ...

    ...

    Filename:primes.py

    Line# Mem usage Increment Line Contents

    2 @profile

    3 7.9219MB 0.0000MB defprimes(n):

    4 7.9219MB 0.0000MB ifn==2:

    5 return[2]

    6 7.9219MB 0.0000MB elifn<2:

    7 return

    8 7.9219MB 0.0000MB s=range(3n+12)

    9 7.9258MB 0.0039MB mroot=n**0.5

    10 7.9258MB 0.0000MB half=(n+1)/2-1

    11 7.9258MB 0.0000MB i=0

    12 7.9258MB 0.0000MB m=3

    13 7.9297MB 0.0039MB whilem<=mroot:

    14 7.9297MB 0.0000MB ifs[i]:

    15 7.9297MB 0.0000MB j=(m*m-3)/2

    16 7.9258MB-0.0039MB s[j]=0

    17 7.9297MB 0.0039MB whilej<half:

    18 7.9297MB 0.0000MB s[j]=0

    19 7.9297MB 0.0000MB j+=m

    20 7.9297MB 0.0000MB i=i+1

    21 7.9297MB 0.0000MB m=2*i+3

    22 7.9297MB 0.0000MB return[2]+[xforxinsifx]

    line_profiler 和 memory_profiler 一个鲜为人知的特性就是在 IPython 上都有快捷命令。你所能做的就是在 IPython 上键入以下命令:

    %load_extmemory_profiler

    这样做了以后,你就可以使用魔法命令 %lprun 和 %mprun 了,它们表现的像它们命令行的副本,最主要的不同就是你不需要给你需要分析的函数设置 @profile 修饰符。直接在你的 IPython 会话上继续分析吧。

    这可以节省你大量的时间和精力,因为使用这些分析命令,你不需要修改你的源代码。

    cPython的解释器使用引用计数来作为它跟踪内存的主要方法。这意味着每个对象持有一个计数器,当增加某个对象的引用存储的时候,计数器就会增加,当一个引用被删除的时候,计数器就是减少。当计数器达到0, cPython 解释器就知道该对象不再使用,因此解释器将删除这个对象,并且释放该对象持有的内存。

    内存泄漏往往发生在即使该对象不再使用的时候,你的程序还持有对该对象的引用。

    最快速发现内存泄漏的方式就是使用一个由 Marius Gedminas 编写的非常好的称为 [objgraph][6] 的工具。

    这个工具可以让你看到在内存中对象的数量,也定位在代码中所有不同的地方,对这些对象的引用。

    MyBigFatObject 20000

    tuple 169382881064151

    function 4310

    dict2790

    method_descriptor 507

    getset_descriptor 451

    哪个对象被增加或是删除了?

    .

    .

    .

    (pdb)objgraph.show_growth # this only shows objects that has been added or deleted since last show_growth call

    traceback 4 +2

    KeyboardInterrupt 1 +1

    frame 24 +1

    list 667 +1

    tuple 16969 +1

    这个泄漏对象的引用是什么?

    为了看到持有变量 X 的引用是什么,运行 objgraph.show_backref 函数:

    (pdb)import objgraph

  • 相关阅读:
    Mvc 简单分页代码
    算法
    atx
    Java8函数式编程(A)
    axios
    props
    vue 的keep alive使用注意项
    android帮助
    testng监听器方法执行顺序
    常用正则表达式
  • 原文地址:https://www.cnblogs.com/cbryge/p/6189293.html
Copyright © 2020-2023  润新知