python代码性能分析_如何进行python性能分析

展开全部

使用time工具粗糙定时

首先,我们可以62616964757a686964616fe59b9ee7ad9431333361306366使用快速然而粗糙的工具:古老的unix工具time,来为我们的代码检测运行时间。

1 $ time python yourprogram.py

2

3 real 0m1.028s

4 user 0m0.001s

5 sys 0m0.003s

上面三个输入变量的意义在文章 stackoverflow article 中有详细介绍。简单的说:

real - 表示实际的程序运行时间

user - 表示程序在用户态的cpu总时间

sys - 表示在内核态的cpu总时间

通过sys和user时间的求和,你可以直观的得到系统上没有其他程序运行时你的程序运行所需要的CPU周期。

若sys和user时间之和远远少于real时间,那么你可以猜测你的程序的主要性能问题很可能与IO等待相关。

使用计时上下文管理器进行细粒度计时

我们的下一个技术涉及访问细粒度计时信息的直接代码指令。这是一小段代码,我发现使用专门的计时测量是非常重要的:

timer.py

01 import time

02

03 class Timer(object):

04 def __init__(self, verbose=False):

05 self.verbose = verbose

06

07 def __enter__(self):

08 self.start = time.time()

09 return self

10

11 def __exit__(self, *args):

12 self.end = time.time()

13 self.secs = self.end - self.start

14 self.msecs = self.secs * 1000 # millisecs

15 if self.verbose:

16 print 'elapsed time: %f ms' % self.msecs

为了使用它,你需要用Python的with关键字和Timer上下文管理器包装想要计时的代码块。它将会在你的代码块开始执行的时候启动计时器,在你的代码块结束的时候停止计时器。

这是一个使用上述代码片段的例子:

01 from timer import Timer

02 from redis import Redis

03 rdb = Redis()

04

05 with Timer() as t:

06 rdb.lpush("foo", "bar")

07 print "=> elasped lpush: %s s" % t.secs

08

09 with Timer as t:

10 rdb.lpop("foo")

11 print "=> elasped lpop: %s s" % t.secs

我经常将这些计时器的输出记录到文件中,这样就可以观察我的程序的性能如何随着时间进化。

使用分析器逐行统计时间和执行频率

Robert Kern有一个称作line_profiler的不错的项目,我经常使用它查看我的脚步中每行代码多快多频繁的被执行。

想要使用它,你需要通过pip安装该python包:

1 $ pip install line_profiler

一旦安装完成,你将会使用一个称做“line_profiler”的新模组和一个“kernprof.py”可执行脚本。

想要使用该工具,首先修改你的源代码,在想要测量的函数上装饰@profile装饰器。不要担心,你不需要导入任何模组。kernprof.py脚本将会在执行的时候将它自动地注入到你的脚步的运行时。

primes.py

01 @profile

02 def primes(n):

03 if n==2:

04 return [2]

05 elif n<2:

06 return []

07 s=range(3,n+1,2)

08 mroot = n ** 0.5

09 half=(n+1)/2-1

10 i=0

11 m=3

12 while m <= mroot:

13 if s[i]:

14 j=(m*m-3)/2

15 s[j]=0

16 while j

17 s[j]=0

18 j+=m

19 i=i+1

20 m=2*i+3

21 return [2]+[x for x in s if x]

22 primes(100)

一旦你已经设置好了@profile装饰器,使用kernprof.py执行你的脚步。

1 $ kernprof.py -l -v fib.py

-l选项通知kernprof注入@profile装饰器到你的脚步的内建函数,-v选项通知kernprof在脚本执行完毕的时候显示计时信息。上述脚本的输出看起来像这样:

01 Wrote profile results to primes.py.lprof

02 Timer unit: 1e-06 s

03

04 File: primes.py

05 Function: primes at line 2

06 Total time: 0.00019 s

07

08 Line # Hits Time Per Hit % Time Line Contents

09 ==============================================================

10 2 @profile

11 3 def primes(n):

12 4 1 2 2.0 1.1 if n==2:

13 5 return [2]

14 6 1 1 1.0 0.5 elif n<2:

15 7 return []

16 8 1 4 4.0 2.1 s=range(3,n+1,2)

17 9 1 10 10.0 5.3 mroot = n ** 0.5

18 10 1 2 2.0 1.1 half=(n+1)/2-1

19 11 1 1 1.0 0.5 i=0

20 12 1 1 1.0 0.5 m=3

21 13 5 7 1.4 3.7 while m <= mroot:

22 14 4 4 1.0 2.1 if s[i]:

23 15 3 4 1.3 2.1 j=(m*m-3)/2

24 16 3 4 1.3 2.1 s[j]=0

25 17 31 31 1.0 16.3 while j

26 18 28 28 1.0 14.7 s[j]=0

27 19 28 29 1.0 15.3 j+=m

28 20 4 4 1.0 2.1 i=i+1

29 21 4 4 1.0 2.1 m=2*i+3

30 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安装:

1 $ pip install -U memory_profiler

2 $ pip install psutil

(这里建议安装psutil包,因为它可以大大改善memory_profiler的性能)。

就像line_profiler,memory_profiler也需要在感兴趣的函数上面装饰@profile装饰器:

1 @profile

2 def primes(n):

3 ...

4 ...

想要观察你的函数使用了多少内存,像下面这样执行:

1 $ python -m memory_profiler primes.py

一旦程序退出,你将会看到看起来像这样的输出:

01 Filename: primes.py

02

03 Line # Mem usage Increment Line Contents

04 ==============================================

05 2 @profile

06 3 7.9219 MB 0.0000 MB def primes(n):

07 4 7.9219 MB 0.0000 MB if n==2:

08 5 return [2]

09 6 7.9219 MB 0.0000 MB elif n<2:

10 7 return []

11 8 7.9219 MB 0.0000 MB s=range(3,n+1,2)

12 9 7.9258 MB 0.0039 MB mroot = n ** 0.5

13 10 7.9258 MB 0.0000 MB half=(n+1)/2-1

14 11 7.9258 MB 0.0000 MB i=0

15 12 7.9258 MB 0.0000 MB m=3

16 13 7.9297 MB 0.0039 MB while m <= mroot:

17 14 7.9297 MB 0.0000 MB if s[i]:

18 15 7.9297 MB 0.0000 MB j=(m*m-3)/2

19 16 7.9258 MB -0.0039 MB s[j]=0

20 17 7.9297 MB 0.0039 MB while j

21 18 7.9297 MB 0.0000 MB s[j]=0

22 19 7.9297 MB 0.0000 MB j+=m

23 20 7.9297 MB 0.0000 MB i=i+1

24 21 7.9297 MB 0.0000 MB m=2*i+3

25 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会话中输入以下内容:

1 %load_ext memory_profiler

2 %load_ext line_profiler

在这样做的时候你需要访问魔法命令%lprun和%mprun,它们的行为类似于他们的命令行形式。主要区别是你不需要使用@profiledecorator来修饰你要分析的函数。只需要在IPython会话中像先前一样直接运行分析:

1 In [1]: from primes import primes

2 In [2]: %mprun -f primes primes(1000)

3 In [3]: %lprun -f primes primes(1000)

这样可以节省你很多时间和精力,因为你的源代码不需要为使用这些分析命令而进行修改。

内存泄漏在哪里?

cPython解释器使用引用计数做为记录内存使用的主要方法。这意味着每个对象包含一个计数器,当某处对该对象的引用被存储时计数器增加,当引用被删除时计数器递减。当计数器到达零时,cPython解释器就知道该对象不再被使用,所以删除对象,释放占用的内存。

如果程序中不再被使用的对象的引用一直被占有,那么就经常发生内存泄漏。

查找这种“内存泄漏”最快的方式是使用Marius Gedminas编写的objgraph,这是一个极好的工具。该工具允许你查看内存中对象的数量,定位含有该对象的引用的所有代码的位置。

本回答由网友推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值