python读取内存 如何测量运行中的程序内存

一个用 heapy 进行 Python 程序内存调试的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
已注册用户请 &
推荐学习书目
Python Sites
值得关注的项目
Python 编程
一个用 heapy 进行 Python 程序内存调试的问题
· 349 天前 · 1045 次点击
本程序现状如下:
(Pdb) h0 = hp.heap()
Partition of a set of 4254865 objects. Total size =
% Cumulative
% Kind (class / dict of class)
61 dict (no owner)
91 unicode
96 _sre.SRE_Pattern
98 dict of module
99 types.CodeType
99 function
&653 more rows. Type e.g. '_.more' to view.&
可以看到,dict 占用了大部分内存。然而这道这点儿当然不够,我还要知道“哪些字典”大,比如,哪个模块里创建的,怎么引用到它,之类问题。
难处就在于,这个情景下,嵌套的字典非常多,给一段代码模拟这个场景:
def rand():
return random.random()
def rand_int(i):
return random.randint(0, i)
def rdt(max_depth, max_width):
if max_depth &= 1:
for i in range(rand_int(max_width)):
r[rand()] = rand()
for i in range(rand_int(max_width)):
r[rand()] = rdt(rand_int(max_depth) - 1, max_width)
t0 = rdt(9, 9)
t1 = rdt(7, 14)
t2 = rdt(5, 19)
t3 = rdt(3, 24)
现在,请问我直接创建的四个 t,哪个大?
“大”当然不是指那一个 PyObject 占用的空间大就行了,而是所有直接间接 referents dicts 都加在一起,才有意义。
所以类似这样的解答不行:
&&& (h0[0] - h0[0].referents).byvia
Partition of a set of 5 objects. Total size = 2168 bytes.
% Cumulative
% Referred Via:
48 "['t4']"
61 "['t0']"
74 "['t1']"
87 "['t2']"
各位牛人,请教了
3 回复 &| &直到
15:46:26 +08:00
& &349 天前
这个问题我只能参观了!
& &349 天前
以前从来没有遇到过这种问题。提供一个思路,你需要 Pympler,每个变量创建前后调用 tracker.SummaryTracker,然后根据变化量就能知道内存占用了。
当然,更直观的方法就是 asizeof
```
&&& from pympler import asizeof
&&& asizeof.asizeof(t0)
4880
```
& &348 天前
多谢 pympler 这个工具推荐
我搜 python memory profile and debug,为什么没搜出这个工具呢,只看到了 line profiler,heapy 啥的
这样搞是一个思路了,很笨的一个思路呵呵,很慢,跑十几分钟才跑出十分之一:
```python
from pympler.asizeof import asizeof
h0 = hp.heap()
ds = h0[0]
# 获取&根&dict set
root_ds = (ds - ds.referrers).byid
# 看一看每个“根 dict ”的 recursive size
sizes = []
for i in range(len(root_ds)):
info = i, asizeof(root_ds[i])
sizes.append(info)
# check size
```
& · & 1124 人在线 & 最高记录 3541 & · &
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.1 · 16ms · UTC 23:50 · PVG 07:50 · LAX 16:50 · JFK 19:50? Do have faith in what you're doing.4.4k 次阅读
标签:至少1个,最多5个
通常我们可以用python profiler去分析应用程序中哪个模块被多次调用和那个程序部分运行的速度较为缓慢,但是并不能够准确给出我们应用程序在运行中在内存中占用的大小。
比如说在金融数据中会操作大量的实际数据驻存到内存中,并对数据空间大小和性能能够有更好的优化,就需要考虑内存的测量,保证不会造成程序在运行中过载的压力引发程序上的异常。
有些情况下在python中定义类class 会开辟更多的内存,尤其是创建更多的实例数据就会内存的大量占用。有些情况下需要在类中定义__slots__来封装属性,但是这样会破坏类中内置的__dict__.
对于大量的数据运算和分析,这时候对内存的分配就需要额外的关注和分析,能够得到程序在运行时的内存方面的统计能够更好的提升程序的性能。
Valgrind 能够动态分析程序性能的工具,能够自动测量内存管理和线程中的Bug,给出更详细的测量工具。
内存错误探测
双线程错误探测
缓存和分支结构分析器
程序调用结构缓存和分支结构分析器
简单介绍一下Valgrind,更详细的内容可以参考.
如何用来分析python方面程序的性能呢?
比如在做大量数据运算和分析上,用pytables读取大量数据,pytables的数据存贮是采用HDF5存贮结构进行数据存贮,是支持大量数据读取和性能上有一定的提升相对于数据库系统,但是并不能是一个有效的数据库系统,这里不要误解,HDF5是对某些数据的高速读取和录入上有很好的性能,不是一个关系型数据库也不是NoSQL数据库。HDF5玩起来非常的不容易。这里采用了两个库numpy和 pytables,这里在HDF5创建50个array dataset,每个array的长度为。python的代码:
import tables
import numpy as np
h5file = tables.openFile('test4.h5', mode='w', title="Test Array")
array_len =
arrays = np.arange(50)
for x in arrays:
x_a = np.zeros(array_len, dtype=float)
h5file.createArray(h5file.root, "test" + str(x), x_a)
h5file.close()
如何分析性能
massif 能够检测系统中的内存占用情况。我们将上述的代码保持成test.py
valgrind --tool=massif python test.py
然后我们就会生成一个massif.out.???中的内容,然后我们可以用 ms_print 命令。
massif.out.12076 > profile.txt
...,....@::::@@
0 +-----------------------------------------------------------------------&Gi
程序在启动中大概占用了500m的内存容量,通过图表我们可以直观的看到内存的数据增长,并且能够有效的理解内存上的分配数据。
后续我们就可以通过代码上的优化调整内存中的数据和性能。
0 收藏&&|&&6
博主头像亮了。。。
博主头像亮了。。。
分享到微博?
我要该,理由是:
在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。检测Python程序执行效率及内存和CPU使用的7种方法
在运行复杂的Python程序时,执行时间会很长,这时也许想提高程序的执行效率。但该怎么做呢?
首先,要有个工具能够检测代码中的瓶颈,例如,找到哪一部分执行时间比较长。接着,就针对这一部分进行优化。
同时,还需要控制内存和CPU的使用,这样可以在另一方面优化代码。
因此,在这篇文章中我将介绍7个不同的Python工具,来检查代码中函数的执行时间以及内存和CPU的使用。
1. 使用装饰器来衡量函数执行时间
有一个简单方法,那就是定义一个装饰器来测量函数的执行时间,并输出结果:
functools import
fn_timer(function):
&&&&@wraps(function)
function_timer(*args,
**kwargs):
&&&&&&&&t0
time.time()
&&&&&&&&result
function(*args,
&&&&&&&&t1
time.time()
&&&&&&&&print
time running %s: %s seconds&
&&&&&&&&&&&&&&&&(function.func_name,
str(t1-t0))
&&&&&&&&&&&&&&&&)
&&&&&&&&return
&&&&return
function_timer
接着,将这个装饰器添加到需要测量的函数之前,如下所示:
myfunction(...):
例如,这里检测一个函数排序含有200万个随机数字的数组所需的时间:
random_sort(n):
&&&&return
sorted([random.random()
range(n)])
__name__ ==
&__main__&:
&&&&random_sort(2000000)
执行脚本时,会看到下面的结果:
time running random_sort: 1. seconds
2. 使用timeit模块
另一种方法是使用timeit模块,用来计算平均时间消耗。
执行下面的脚本可以运行该模块。
timing_functions&
&timing_functions.random_sort(2000000)&
这里的timing_functions是Python脚本文件名称。
在输出的末尾,可以看到以下结果:
loops, best of 5:
sec per loop
这表示测试了4次,平均每次测试重复5次,最好的测试结果是2.08秒。
如果不指定测试或重复次数,默认值为10次测试,每次重复5次。
3. 使用Unix系统中的time命令
然而,装饰器和timeit都是基于Python的。在外部环境测试Python时,unix time实用工具就非常有用。
运行time实用工具:
python timing_functions.py
输出结果为:
time running random_sort: 1.
第一行来自预定义的装饰器,其他三行为:
注意:根据,内核是一个计算机程序,用来管理软件的输入输出,并将其翻译成CPU和其他计算机中的电子设备能够执行的数据处理指令。
因此,Real执行时间和User+Sys执行时间的差就是消耗在输入/输出和系统执行其他任务时消耗的时间。
4. 使用cProfile模块
如果想知道每个函数和方法消耗了多少时间,以及这些函数被调用了多少次,可以使用cProfile模块。
cProfile -s
cumulative timing_functions.py
现在可以看到代码中函数的详细描述,其中含有每个函数调用的次数,由于使用了-s选项(累加),最终结果会根据每个函数的累计执行时间排序。
读者会发现执行脚本所需的总时间比以前要多。这是由于测量每个函数的执行时间这个操作本身也是需要时间。
5. 使用line_profiler模块
line_profiler模块可以给出执行每行代码所需占用的CPU时间。
首先,安装该模块:
pip install line_profiler
接着,需要指定用@profile检测哪个函数(不需要在代码中用import导入模块):
random_sort2(n):
[random.random() for
&&&&l.sort()
&&&&return
__name__ ==
&__main__&:
&&&&random_sort2(2000000)
最好,可以通过下面的命令获得关于random_sort2函数的逐行描述。
kernprof -l
timing_functions.py
其中-l表示逐行解释,-v表示表示输出详细结果。通过这种方法,我们看到构建数组消耗了44%的计算时间,而sort()方法消耗了剩余的56%的时间。
同样,由于需要检测执行时间,脚本的执行时间更长了。
6. 使用memory_profiler模块
memory_profiler模块用来基于逐行测量代码的内存使用。使用这个模块会让代码运行的更慢。
安装方法如下:
pip install memory_profiler
另外,建议安装psutil包,这样memory_profile会运行的快一点:
pip install psutil
与line_profiler相似,使用@profile装饰器来标识需要追踪的函数。接着,输入:
memory_profiler timing_functions.py
脚本的执行时间比以前长1或2秒。如果没有安装psutil包,也许会更长。
从结果可以看出,内存使用是以MiB为单位衡量的,表示的mebibyte(1MiB = 1.05MB)。
7. 使用guppy包
最后,通过这个包可以知道在代码执行的每个阶段中,每种类型(str、tuple、dict等)分别创建了多少对象。
安装方法如下:
pip install guppy
接着,将其添加到代码中:
guppy import
random_sort3(n):
&Heap at the beginning of the functionn&,
[random.random() for
&&&&l.sort()
&Heap at the end of the functionn&,
&&&&return
__name__ ==
&__main__&:
&&&&random_sort3(2000000)
运行代码:
python timing_functions.py
可以看到输出结果为:
通过在代码中将heap()放置在不同的位置,可以了解到脚本中的对象创建和删除操作的流程。
如果想学习更多关于Python代码速度优化方面的知识,我建议你去读这本书《》
希望这篇文章能偶帮到你!^_^
&&& Python频道微信号:PythonCoder, 扫描加关注,碎片时间提升Python开发技能!
没有更多推荐了,此文原始版本转自互联网,本文作者进行代码验证后,略有删改
代码验证环境如下
因此,在这篇文章中我将介绍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
接着,将这个装饰器添加到需要测量的函数之前,如下所示:&
def myfunction(...):
例如,这里检测一个函数排序含有200万个随机数字的数组所需的时间:&
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. 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":
"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"}
{"capability": {"get_input": "capability"}, "forwarder": "VNFFG_VNFD1"},
{"capability": "CP22", "forwarder": "VNFFG_VNFD2"}
"description": {"get_input": "description"},
"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
def dict_matched001(src_dict, target_dict):
for param_key in src_dict.keys():
if target_dict.get(param_key) is None:
def dict_matched002(src_dict, target_dict):
unmatch = set(src_dict) - set(target_dict)
if unmatch:
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__":
执行脚本时,会看到下面的结果:
Start to check dict_matched001 running time.
Total time running dict_matched001: 5.e-06 seconds
Start to check dict_matched002 running time.
Total time running dict_matched002: 1.e-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. seconds
第一行来自预定义的装饰器,其他三行为:
&&& real表示的是执行脚本的总时间
&&& user表示的是执行脚本消耗的CPU时间。
&&& sys表示的是执行内核函数消耗的时间。
注意:根据,内核是一个计算机程序,用来管理软件的输入输出,并将其翻译成CPU和其他计算机中的电子设备能够执行的数据处理指令。
因此,Real执行时间和User+Sys执行时间的差就是消耗在输入/输出和系统执行其他任务时消耗的时间。4. 使用cProfile模块
如果想知道每个函数和方法消耗了多少时间,以及这些函数被调用了多少次,可以使用cProfile模块。&
$ python -m cProfile -s cumulative timing_functions.py
现在可以看到代码中函数的详细描述,其中含有每个函数调用的次数,由于使用了-s选项(累加),最终结果会根据每个函数的累计执行时间排序。
读者会发现执行脚本所需的总时间比以前要多。这是由于测量每个函数的执行时间这个操作本身也是需要时间。5. 使用line_profiler模块
line_profiler模块可以给出执行每行代码所需占用的CPU时间。
首先,安装该模块:&
$ pip install line_profiler
接着,需要指定用@profile检测哪个函数(不需要在代码中用import导入模块):&
def random_sort2(n):
l = [random.random() for i in range(n)]
if __name__ == "__main__":
random_sort2(2000000)
最好,可以通过下面的命令获得关于random_sort2函数的逐行描述。&
$ kernprof -l -v timing_functions.py
其中-l表示逐行解释,-v表示表示输出详细结果。通过这种方法,我们看到构建数组消耗了44%的计算时间,而sort()方法消耗了剩余的56%的时间。
同样,由于需要检测执行时间,脚本的执行时间更长了。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包,也许会更长。
从结果可以看出,内存使用是以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)]
print "Heap at the end of the functionn", hp.heap()
if __name__ == "__main__":
random_sort3(2000000)
运行代码:&
$ python timing_functions.py
可以看到输出结果为:
通过在代码中将heap()放置在不同的位置,可以了解到脚本中的对象创建和删除操作的流程。
如果想学习更多关于Python代码速度优化方面的知识,有时间可以去看看这本书
阅读(...) 评论()检测Python程序执行效率及内存和CPU使用的7种方法 - Python - 伯乐在线
& 检测Python程序执行效率及内存和CPU使用的7种方法
在运行复杂的Python程序时,执行时间会很长,这时也许想提高程序的执行效率。但该怎么做呢?
首先,要有个工具能够检测代码中的瓶颈,例如,找到哪一部分执行时间比较长。接着,就针对这一部分进行优化。
同时,还需要控制内存和CPU的使用,这样可以在另一方面优化代码。
因此,在这篇文章中我将介绍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
1234567891011121314
import timefrom 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
接着,将这个装饰器添加到需要测量的函数之前,如下所示:
def myfunction(...):
@fn_timerdef myfunction(...):...
例如,这里检测一个函数排序含有200万个随机数字的数组所需的时间:
def random_sort(n):
return sorted([random.random() for i in range(n)])
if __name__ == "__main__":
random_sort(2000000)
@fn_timerdef random_sort(n):&&&&return sorted([random.random() for i in range(n)])&if __name__ == "__main__":&&&&random_sort(2000000)
执行脚本时,会看到下面的结果:
Total time running random_sort: 1. seconds
Total time running random_sort: 1. seconds
2. 使用timeit模块
另一种方法是使用timeit模块,用来计算平均时间消耗。
执行下面的脚本可以运行该模块。
$ python -m timeit -n 4 -r 5 -s "import timing_functions" "timing_functions.random_sort(2000000)"
$ 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 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
$ time -p python timing_functions.py
输出结果为:
Total time running random_sort: 1. seconds
Total time running random_sort: 1. secondsreal 1.49user 1.40sys 0.08
第一行来自预定义的装饰器,其他三行为:
real表示的是执行脚本的总时间
user表示的是执行脚本消耗的CPU时间。
sys表示的是执行内核函数消耗的时间。
注意:根据,内核是一个计算机程序,用来管理软件的输入输出,并将其翻译成CPU和其他计算机中的电子设备能够执行的数据处理指令。
因此,Real执行时间和User+Sys执行时间的差就是消耗在输入/输出和系统执行其他任务时消耗的时间。
4. 使用cProfile模块
如果想知道每个函数和方法消耗了多少时间,以及这些函数被调用了多少次,可以使用cProfile模块。
$ python -m cProfile -s cumulative timing_functions.py
$ python -m cProfile -s cumulative timing_functions.py
现在可以看到代码中函数的详细描述,其中含有每个函数调用的次数,由于使用了-s选项(累加),最终结果会根据每个函数的累计执行时间排序。
读者会发现执行脚本所需的总时间比以前要多。这是由于测量每个函数的执行时间这个操作本身也是需要时间。
5. 使用line_profiler模块
line_profiler模块可以给出执行每行代码所需占用的CPU时间。
首先,安装该模块:
$ pip install line_profiler
$ pip install line_profiler
接着,需要指定用@profile检测哪个函数(不需要在代码中用import导入模块):
def random_sort2(n):
l = [random.random() for i in range(n)]
if __name__ == "__main__":
random_sort2(2000000)
@profiledef 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
$ kernprof -l -v timing_functions.py
其中-l表示逐行解释,-v表示表示输出详细结果。通过这种方法,我们看到构建数组消耗了44%的计算时间,而sort()方法消耗了剩余的56%的时间。
同样,由于需要检测执行时间,脚本的执行时间更长了。
6. 使用memory_profiler模块
memory_profiler模块用来基于逐行测量代码的内存使用。使用这个模块会让代码运行的更慢。
安装方法如下:
$ pip install memory_profiler
$ pip install memory_profiler
另外,建议安装psutil包,这样memory_profile会运行的快一点:
$ pip install psutil
$ pip install psutil
与line_profiler相似,使用@profile装饰器来标识需要追踪的函数。接着,输入:
$ python -m memory_profiler timing_functions.py
$ python -m memory_profiler timing_functions.py
脚本的执行时间比以前长1或2秒。如果没有安装psutil包,也许会更长。
从结果可以看出,内存使用是以MiB为单位衡量的,表示的mebibyte(1MiB = 1.05MB)。
7. 使用guppy包
最后,通过这个包可以知道在代码执行的每个阶段中,每种类型(str、tuple、dict等)分别创建了多少对象。
安装方法如下:
$ pip install guppy
$ 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)]
print "Heap at the end of the functionn", hp.heap()
if __name__ == "__main__":
random_sort3(2000000)
123456789101112
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
$ python timing_functions.py
可以看到输出结果为:
通过在代码中将heap()放置在不同的位置,可以了解到脚本中的对象创建和删除操作的流程。
如果想学习更多关于Python代码速度优化方面的知识,我建议你去读这本书《》
希望这篇文章能偶帮到你!^_^
打赏支持我翻译更多好文章,谢谢!
打赏支持我翻译更多好文章,谢谢!
任选一种支付方式
关于作者:}

我要回帖

更多关于 python 内存映射 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信