[总结]python逐行性能分析

总是忘记,做个记录

性能定义

一般而言,处理一个请求的速度是一个重要的性能指标(Latency, 系统延迟)。

观察工具

有时候不需要特殊工具,直接看一个http请求的时长即可。

定位瓶颈

line_profiler是一个可以查看每行代码的执行次数、执行时间的工具。

安装好后,可以在代码中对方法/函数添加装饰器@profile,然后在命令行使用kernprof -l -v xx.py来查看结果。

这样太麻烦,可以直接在IPython中直接使用。

导入:

In [1]: %load_ext line_profiler

help:

In [2]: %lprun?
Docstring:
Execute a statement under the line-by-line profiler from the
line_profiler module.

Usage:
  %lprun -f func1 -f func2 

The given statement (which doesn't require quote marks) is run via the
LineProfiler. Profiling is enabled for the functions specified by the -f
options. The statistics will be shown side-by-side with the code through the
pager once the statement has completed.

Options:

-f : LineProfiler only profiles functions and methods it is told
to profile.  This option tells the profiler about these functions. Multiple
-f options may be used. The argument may be any expression that gives
a Python function or method object. However, one must be careful to avoid
spaces that may confuse the option parser. Additionally, functions defined
in the interpreter at the In[] prompt or via %run currently cannot be
displayed.  Write these functions out to a separate file and import them.

-m : Get all the functions/methods in a module

One or more -f or -m options are required to get any useful results.

-D : dump the raw statistics out to a pickle file on disk. The
usual extension for this is ".lprof". These statistics may be viewed later
by running line_profiler.py as a script.

-T : dump the text-formatted statistics with the code side-by-side
out to a text file.

-r: return the LineProfiler object after it has completed profiling.

-s: strip out all entries from the print-out that have zeros.
File:      /Library/Python/2.7/site-packages/line_profiler.py

举例:
在exmaple.py中:

class Demo:
    def method1(self):
        self.method2()

    def method2(self):
        pass

也可直接使用IPython的%%writefile来直接写一个。

查看运行情况,其中每个-f后都可加一个函数/方法,用于执行情况。

from example import Demo
demo = Demo()
%lprun -f demo.method1 demo.method1()
%lprun -f demo.method1 -f demo.method2 demo.method1()

这样就能找到瓶颈啦!

你可能感兴趣的:([总结]python逐行性能分析)