首页 技术 正文
技术 2022年11月20日
0 收藏 787 点赞 3,007 浏览 2242 个字

操作系统 : 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 sumif __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 namencalls  tottime  percall  cumtime  percall filename:lineno(function)
0.000 0.000 0.000 0.000 <string>:(<module>)
0.000 0.000 0.000 0.000 cProfileTest2.py:(foo)
0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
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 = [] * (*)
b = [] * (*)
del b
return a

使用memory_profiler是需要修改代码的,这里记录下以下两种使用方式:

1、不导入模块使用

@profile
def my_func():
a = [] * (*)
b = [] * (*)
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 = [] * (*)
b = [] * (*)
del b
return a

完整代码如下:

直接运行程序即可进行分析。

运行效果如下:

(py27env) [mike@local test]$ python test1.py
Filename: test1.pyLine # Mem usage Increment Line Contents
================================================
29.5 MiB 0.0 MiB @profile
def my_func():
29.5 MiB 0.0 MiB a = [] * (*)
29.5 MiB 0.0 MiB b = [] * (*)
29.5 MiB 0.0 MiB del b
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

欢迎补充

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,030
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,859