python程序之profile分析


操作系統 : CentOS7.3.1611_x64

python版本:2.7.5

問題描述

1、Python開發的程序在使用過程中很慢,想確定下是哪段代碼比較慢;

2、Python開發的程序在使用過程中占用內存很大,想確定下是哪段代碼引起的;

解決方案

使用profile分析分析cpu使用情況

profile介紹: https://docs.python.org/2/library/profile.html

可以使用profile和cProfile對python程序進行分析,這里主要記錄下cProfile的使用,profile參考cProfile即可。

假設有如下代碼需要進行分析(cProfileTest1.py):

#! /usr/bin/env python
#-*- coding:utf-8 -*-

def foo():
    sum = 0
    for i in range(100):
        sum += i
    return sum

if __name__ == "__main__" :
    foo()

可以通過以下兩種使用方式進行分析:

1、不修改程序

分析程序:

python -m cProfile -o test1.out cProfileTest1.py

查看運行結果:

python -c "import pstats; p=pstats.Stats('test1.out'); p.print_stats()"

查看排序后的運行結果:

python -c "import pstats; p=pstats.Stats('test1.out'); p.sort_stats('time').print_stats()"

2、修改程序

加入如下代碼:

import cProfile
cProfile.run("foo()")

完整代碼如下: https://github.com/mike-zhang/pyExamples/blob/master/profileOpt/cpuProfile1/cProfileTest2.py

運行效果如下:

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.000    0.000 <string>:1(<module>)
     1    0.000    0.000    0.000    0.000 cProfileTest2.py:4(foo)
     1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
     1    0.000    0.000    0.000    0.000 {range}

結果說明:

ncalls : 函數的被調用次數
tottime :函數總計運行時間,除去函數中調用的函數運行時間
percall :函數運行一次的平均時間,等於tottime/ncalls
cumtime :函數總計運行時間,含調用的函數運行時間
percall :函數運行一次的平均時間,等於cumtime/ncalls
filename:lineno(function) 函數所在的文件名,函數的行號,函數名

使用memory_profiler分析內存使用情況

https://pypi.python.org/pypi/memory_profiler

需要安裝memory_profiler :

pip install psutil
pip install memory_profiler

假設有如下代碼需要進行分析:

def my_func():
    a = [1] * (10*6)
    b = [2] * (10*7)
    del b
    return a

使用memory_profiler是需要修改代碼的,這里記錄下以下兩種使用方式:

1、不導入模塊使用

@profile
def my_func():
    a = [1] * (10*6)
    b = [2] * (10*7)
    del b
    return a

完整代碼如下: https://github.com/mike-zhang/pyExamples/blob/master/profileOpt/memoryProfile1/test1.py

profile分析:

python -m memory_profiler test1.py

2、導入模塊使用

from memory_profiler import profile

@profile
def my_func():
    a = [1] * (10*6)
    b = [2] * (10*7)
    del b
    return a

完整代碼如下:

直接運行程序即可進行分析。

運行效果如下:

(py27env) [mike@local test]$ python test1.py
Filename: test1.py

Line #    Mem usage    Increment   Line Contents
================================================
     6     29.5 MiB      0.0 MiB   @profile
     7                             def my_func():
     8     29.5 MiB      0.0 MiB       a = [1] * (10*6)
     9     29.5 MiB      0.0 MiB       b = [2] * (10*7)
    10     29.5 MiB      0.0 MiB       del b
    11     29.5 MiB      0.0 MiB       return a

profile分析完整代碼地址:https://github.com/mike-zhang/pyExamples/tree/master/profileOpt

好,就這些了,希望對你有幫助。

本文github地址:

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20170907_python程序之profile分析.rst

歡迎補充


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM