[總結(jié)]python逐行性能分析

總是忘記,做個記錄。

性能定義

一般而言,處理一個請求的速度是一個重要的性能指標(Latency, 系統(tǒng)延遲)。

觀察工具

有時候不需要特殊工具,直接看一個http請求的時長即可。

定位瓶頸

line_profiler是一個可以查看每行代碼的執(zhí)行次數(shù)、執(zhí)行時間的工具。

安裝好后,可以在代碼中對方法/函數(shù)添加裝飾器@profile,然后在命令行使用kernprof -l -v xx.py來查看結(jié)果。

這樣太麻煩,可以直接在IPython中直接使用。

導(dǎo)入:

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 <statement>

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 <function>: 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 <module>: Get all the functions/methods in a module

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

-D <filename>: 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 <filename>: 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后都可加一個函數(shù)/方法,用于執(zhí)行情況。

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

這樣就能找到瓶頸啦!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容