python查看函数消耗的内存_(网摘)10种检测Python程序运行时间、CPU和内存占用的方法...

此文原始版本转自互联网,本文作者进行代码验证后,略有删改

代码验证环境如下

1130544-20170411100125672-301261050.png

因此,在这篇文章中我将介绍7个不同的Python工具,来检查代码中函数的执行时间以及内存和CPU的使用。

1. 使用装饰器来衡量函数执行时间

有一个简单方法,那就是定义一个装饰器来测量函数的执行时间,并输出结果:

import time

from functools import wraps

def fn_timer(function):

@wraps(function)

def function_timer(*args, **kwargs):

t0 = time.time()

result = function(*args, **kwargs)

t1 = time.time()

print ("Total time running %s: %s seconds" %

(function.func_name, str(t1-t0))

)

return result

return function_timer

接着,将这个装饰器添加到需要测量的函数之前,如下所示:

@fn_timer

def myfunction(...):

...

例如,这里检测一个函数排序含有200万个随机数字的数组所需的时间:

@fn_timer

def random_sort(n):

return sorted([random.random() for i in range(n)])

if __name__ == "__main__":

random_sort(2000000)

执行脚本时,会看到下面的结果:

Total time running random_sort: 1.41124916077 seconds

1.2 使用<装饰器范例2

执行脚本时,会看到下面的结果:

import time

from functools import wraps

template_db = {

"node_templates":

{

"Forwarding_path1":

{

"type": "tosca.nodes.nfv.FP.Tacker",

"description": "creates path (CP12->CP22)",

"properties":

{

"policy":

{

"type": "ACL",

"criteria":

[

{"network_src_port_id": {"get_input": "network_src_port_id"}},

{"destination_port_range": "80-1024"},

{"ip_proto": 6},

{"ip_dst_prefix": "10.10.0.100/24"}

]

},

"path":

[

{"capability": {"get_input": "capability"}, "forwarder": "VNFFG_VNFD1"},

{"capability": "CP22", "forwarder": "VNFFG_VNFD2"}

],

"id": 51

}

}

},

"description": {"get_input": "description"},

"groups":

{

"VNFFG1":

{

"type": "tosca.groups.nfv.VNFFG",

"description": "HTTP to Corporate Net",

"members": ["Forwarding_path1"],

"properties":

{

"vendor": "tacker",

"connection_point": ["CP12", "CP22"],

"version": 1.0,

"constituent_vnfs": ["VNFFG_VNFD1", "VNFFG_VNFD2"],

"number_of_endpoints": 5,

"dependent_virtual_link": ["VL12", "VL22"]

}

}

}

}

def fn_timer(function):

@wraps(function)

def function_timer(*args, **kwargs):

t0 = time.time()

result = function(*args, **kwargs)

t1 = time.time()

print ("Total time running %s: %s seconds" %

(function.func_name, str(t1-t0)))

return result

return function_timer

@fn_timer

def dict_matched001(src_dict, target_dict):

for param_key in src_dict.keys():

if target_dict.get(param_key) is None:

return

@fn_timer

def dict_matched002(src_dict, target_dict):

unmatch = set(src_dict) - set(target_dict)

if unmatch:

return

g_src_dict = template_db

g_target_dict = template_db

def main():

print ("Start to check dict_matched001 running time.")

dict_matched001(g_src_dict, g_target_dict)

print ("Start to check dict_matched002 running time.")

dict_matched002(g_src_dict, g_target_dict)

if __name__ == "__main__":

main()

执行脚本时,会看到下面的结果:

Start to check dict_matched001 running time.

Total time running dict_matched001: 5.96046447754e-06 seconds

Start to check dict_matched002 running time.

Total time running dict_matched002: 1.4066696167e-05 seconds

2. 使用timeit模块

另一种方法是使用timeit模块,用来计算平均时间消耗。

执行下面的脚本可以运行该模块。

python -m timeit -n 4 -r 5 -s "import timing_functions" "timing_functions.random_sort(2000000)"

这里的timing_functions是Python脚本文件名称。

在输出的末尾,可以看到以下结果:

4 loops, best of 5: 2.08 sec per loop

这表示测试了4次,平均每次测试重复5次,最好的测试结果是2.08秒。

如果不指定测试或重复次数,默认值为10次测试,每次重复5次。

3. 使用Unix系统中的time命令

然而,装饰器和timeit都是基于Python的。在外部环境测试Python时,unix time实用工具就非常有用。

运行time实用工具:

$ time -p python timing_functions.py

输出结果为:

Total time running random_sort: 1.3931210041 seconds

real 1.49

user 1.40

sys 0.08

第一行来自预定义的装饰器,其他三行为:

real表示的是执行脚本的总时间

user表示的是执行脚本消耗的CPU时间。

sys表示的是执行内核函数消耗的时间。

注意:根据维基百科的定义,内核是一个计算机程序,用来管理软件的输入输出,并将其翻译成CPU和其他计算机中的电子设备能够执行的数据处理指令。

因此,Real执行时间和User+Sys执行时间的差就是消耗在输入/输出和系统执行其他任务时消耗的时间。

4. 使用cProfile模块

如果想知道每个函数和方法消耗了多少时间,以及这些函数被调用了多少次,可以使用cProfile模块。

$ python -m cProfile -s cumulative timing_functions.py

现在可以看到代码中函数的详细描述,其中含有每个函数调用的次数,由于使用了-s选项(累加),最终结果会根据每个函数的累计执行时间排序。

201541103516157.jpg?201531103542

读者会发现执行脚本所需的总时间比以前要多。这是由于测量每个函数的执行时间这个操作本身也是需要时间。

5. 使用line_profiler模块

line_profiler模块可以给出执行每行代码所需占用的CPU时间。

首先,安装该模块:

$ pip install line_profiler

接着,需要指定用@profile检测哪个函数(不需要在代码中用import导入模块):

@profile

def random_sort2(n):

l = [random.random() for i in range(n)]

l.sort()

return l

if __name__ == "__main__":

random_sort2(2000000)

最好,可以通过下面的命令获得关于random_sort2函数的逐行描述。

$ kernprof -l -v timing_functions.py

其中-l表示逐行解释,-v表示表示输出详细结果。通过这种方法,我们看到构建数组消耗了44%的计算时间,而sort()方法消耗了剩余的56%的时间。

201541103623876.jpg?201531103641

同样,由于需要检测执行时间,脚本的执行时间更长了。

6. 使用memory_profiler模块

memory_profiler模块用来基于逐行测量代码的内存使用。使用这个模块会让代码运行的更慢。

安装方法如下:

pip install memory_profiler

另外,建议安装psutil包,这样memory_profile会运行的快一点:

$ pip install psutil

与line_profiler相似,使用@profile装饰器来标识需要追踪的函数。接着,输入:

$ python -m memory_profiler timing_functions.py

脚本的执行时间比以前长1或2秒。如果没有安装psutil包,也许会更长。

201541103657211.jpg?201531103716

从结果可以看出,内存使用是以MiB为单位衡量的,表示的mebibyte(1MiB = 1.05MB)。

7. 使用guppy包

最后,通过这个包可以知道在代码执行的每个阶段中,每种类型(str、tuple、dict等)分别创建了多少对象。

安装方法如下:

$ pip install guppy

接着,将其添加到代码中:

from guppy import hpy

def random_sort3(n):

hp = hpy()

print "Heap at the beginning of the functionn", hp.heap()

l = [random.random() for i in range(n)]

l.sort()

print "Heap at the end of the functionn", hp.heap()

return l

if __name__ == "__main__":

random_sort3(2000000)

运行代码:

$ python timing_functions.py

可以看到输出结果为:

201541103732081.jpg?201531103754

通过在代码中将heap()放置在不同的位置,可以了解到脚本中的对象创建和删除操作的流程。

如果想学习更多关于Python代码速度优化方面的知识,有时间可以去看看这本书

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值