文章目录
前言:
这篇文章是转载的!!!
分析一个程序的性能可以归结为一下几个基本的问题:
- 1.它运行的有多快?
- 2.它的速度瓶颈在哪里?
- 3.它使用了多少内存?
- 4.哪里可能发生内训泄露?
下面提供了几个看起来很酷的工作,用来解决这几个问题:
一、使用time模块粗糙定时:
time是古老的unix工具,可以快速,粗略的测试整个代码的运行时间(貌似不太适用于服务):
(base) fei-PC:~/Desktop/learn$ time python test_time.py
start....
end...
real 0m2.010s
user 0m0.009s
sys 0m0.000s
以上三个输出变量的意义在帖子stackoverflow article 中有详细介绍。简单的说:
- real: 表示实际的程序运行时间;
- user: 表示程序在用户态(目态)的cpu总时间;
- sys: 表示在内核态(管态)的cpu总时间;
通过sys和user时间的求和,你可以直观的得到系统上没有其他程序运行时你的程序运行所需要的CPU周期。
若sys和user时间之和远远少于real时间,那么你可以猜测你的程序的主要性能问题很可能与IO等待相关。
二、使用计时上下文管理器进行细粒度计时:
自己实现一个计时上写文管理器,涉及访问细粒度即时信息的直接代码指令。以下是一段代码,用来做专门的计时测量:
timer.py
import time
"""
构造一个上下文管理器
"""
class Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *args, **kwargs):
self.end = time.time()
self.secs = self.end - self.start
self.msecs = self.secs * 1000 # 毫秒值
if self.verbose:
print(f"elapsed time:{self.msecs} ms")
为了使用它,需要在python代码中用with关键字和Timer上下文管理器包装想要计时的代码块. 它将会在代码块开始执行的时候启动计时器,在代码快结束的时候停止计时.
例子:
from time import Timer
with Timer() as t, open('./test.txt',"r") as fin, open('./out.txt',"w") as fout:
fout.write(fin.read())
print "=> elasped lpush: %s s" % t.secs
我经常将这些计时器的输出记录到文件中,这样就可以观察我的程序的性能如何随着时间进化。
三、使用分析器逐行统计时间和执行频率:
Robert Kern有一个称作line_profiler的不错的项目,我经常使用它查看我的脚步中每行代码多快多频繁的被执行。
使用pip安装:
pip install line_profiler
一旦安装完成,你将会使用一个称做“line_profiler”的新模组和一个“kernprof.py”可执行脚本。
想要使用该工具,首先修改你的源代码,在想要测量的函数上装饰@profile装饰器。不要担心,你不需要导入任何模组。kernprof.py脚本将会在执行的时候将它自动地注入到你的脚步的运行时。
test_profile.py
@profile
def primes(n):
if n==2:
return [2]
elif n<2:
return []
s=range(3,n+1,2)
mroot = n ** 0.5
half=(n+1)/2-1
i=0
m=3
while m <= mroot:
if s[i]:
j=(m*m-3)/2
s[j]=0
while j<half:
s[j]=0
j+=m
i=i+1
m=2*i+3
return [2]+[x for x in s if x]
if __name__ == "__main__":
primes(100)
设置好@profile装饰器,使用kernprof.py执行你的脚本。
(base) fei-PC:~/Desktop/learn$ kernprof.py -l -v fib.py
参数:
-l:选项通知kernprof注入@profile装饰器到你的脚步的内建函数,
-v:选项通知kernprof在脚本执行完毕的时候显示计时信息。
上述脚本的输出看起来像这样:
Wrote profile results to primes.py.lprof
Timer unit: 1e-06 s
File: primes.py
Function: primes at line 2
Total time: 0.00019 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
2 @profile
3 def primes(n):
4 1 2 2.0 1.1 if n==2:
5 return [2]
6 1 1 1.0 0.5 elif n<2:
7 return []
8 1 4 4.0 2.1 s=range(3,n+1,2)
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 while m <= mroot:
14 4 4 1.0 2.1 if s[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 while j<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]+[x for x in s if x]
寻找具有高Hits值或高Time值的行。这些就是可以通过优化带来最大改善的地方。
四、内存分析器:
分析完代码的运行时长,可以找到程序的运行瓶颈,但是程序使用了多少内存呢?
Fabian Pedregosa模仿Robert Kern的line_profiler实现了一个不错的内存分析器
使用pip安装:
pip install memory_profiler
pip install psutil
- 这里建议安装psutil包,因为它可以大大改善memory_profiler的性能
就像line_profiler,memory_profiler也需要在感兴趣的函数上面装饰@profile装饰器:
@profile
def primes(n):
...
...
想要观察你的函数使用了多少内存,像下面这样执行:
(base) fei-PC:~/Desktop/learn$ python -m memory_profiler primes.py
一旦程序退出,你将会看到看起来像这样的输出:
Filename: primes.py
Line # Mem usage Increment Line Contents
==============================================
2 @profile
3 7.9219 MB 0.0000 MB def primes(n):
4 7.9219 MB 0.0000 MB if n==2:
5 return [2]
6 7.9219 MB 0.0000 MB elif n<2:
7 return []
8 7.9219 MB 0.0000 MB s=range(3,n+1,2)
9 7.9258 MB 0.0039 MB mroot = n ** 0.5
10 7.9258 MB 0.0000 MB half=(n+1)/2-1
11 7.9258 MB 0.0000 MB i=0
12 7.9258 MB 0.0000 MB m=3
13 7.9297 MB 0.0039 MB while m <= mroot:
14 7.9297 MB 0.0000 MB if s[i]:
15 7.9297 MB 0.0000 MB j=(m*m-3)/2
16 7.9258 MB -0.0039 MB s[j]=0
17 7.9297 MB 0.0039 MB while j<half:
18 7.9297 MB 0.0000 MB s[j]=0
19 7.9297 MB 0.0000 MB j+=m
20 7.9297 MB 0.0000 MB i=i+1
21 7.9297 MB 0.0000 MB m=2*i+3
22 7.9297 MB 0.0000 MB return [2]+[x for x in s if x]
五、line_profiler和memory_profiler的IPython快捷方式:
memory_profiler和line_profiler有一个鲜为人知的小窍门,两者都有在IPython中的快捷命令。你需要做的就是在IPython会话中输入以下内容:
%load_ext memory_profiler
%load_ext line_profiler
在这样做的时候你需要访问魔法命令%lprun和%mprun,它们的行为类似于他们的命令行形式。主要区别是你不需要使用@profiledecorator来修饰你要分析的函数。只需要在IPython会话中像先前一样直接运行分析:
In [1]: from primes import primes
In [2]: %mprun -f primes primes(1000)
In [3]: %lprun -f primes primes(1000)
这样可以节省你很多时间和精力,因为你的源代码不需要为使用这些分析命令而进行修改。
六、内存泄露在哪里?
cPython解释器使用引用计数做为记录内存使用的主要方法。这意味着每个对象包含一个计数器,当某处对该对象的引用被存储时计数器增加,当引用被删除时计数器递减。当计数器到达零时,cPython解释器就知道该对象不再被使用,所以删除对象,释放占用的内存。
如果程序中不再被使用的对象的引用一直被占有,那么就经常发生内存泄漏。
查找这种“内存泄漏”最快的方式是使用Marius Gedminas编写的objgraph,这是一个极好的工具。该工具允许你查看内存中对象的数量,定位含有该对象的引用的所有代码的位置。
- 使用pip安装objectgraph:
pip install objgraph
安装了这个工具,在代码中插入一行声明调用调试器:
import pdb
pdb.set_trace()
执行脚本,进入了一个奇怪的shell功能;
最普遍的对象是哪些?
在运行的时候,你可以通过执行下述指令查看程序中前20个最普遍的对象:
(base) fei-PC:~/Desktop/learn$ python pri.py
> /home/fei/Desktop/learn/pri.py(4)<module>()
-> def primes(n):
(pdb) import objgraph
(pdb) objgraph.show_most_common_types()
MyBigFatObject 20000
tuple 16938
function 4310
dict 2790
wrapper_descriptor 1181
builtin_function_or_method 934
weakref 764
list 634
method_descriptor 507
getset_descriptor 451
type 439
** 哪些对象已经被添加或删除?**
也可以查看两个时间点之间那些对象已经被添加或删除:
(pdb) import objgraph
(pdb) objgraph.show_growth()
.
.
.
(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 = [1]
y = [x, [x], {"a":x}]
import pdb; pdb.set_trace()
想要看看哪里包含变量x的引用,执行objgraph.show_backref()函数:
(pdb) import objgraph
(pdb) objgraph.show_backref([x], filename="/tmp/backrefs.png")
该命令的输出应该是一副PNG图像,保存在/tmp/backrefs.png,它看起来是像这样:
最下面有红字的盒子是我们感兴趣的对象。我们可以看到,它被符号x引用了一次,被列表y引用了三次。如果是x引起了一个内存泄漏,我们可以使用这个方法,通过跟踪它的所有引用,来检查为什么它没有自动的被释放。
回顾一下,objgraph 使我们可以:
- 显示占据python程序内存的头N个对象
- 显示一段时间以后哪些对象被删除活增加了
- 在我们的脚本中显示某个给定对象的所有引用