python memory_profiler库生成器和迭代器内存占用的时间分析
作者:MAR-Sky 发布时间:2023-01-06 21:25:02
标签:python,memory,profiler,生成器,迭代器,内存,占用
不进行计算时,生成器和list空间占用
import time
from memory_profiler import profile
@profile(precision=4)
def list_fun():
start = time.time()
total = ([i for i in range(5000000)])
print('iter_spend_time:',time.time()-start)
@profile(precision=4)
def gent_func():
gent_start = time.time()
total = (i for i in range(5000000))
print('gent_spend_time:',time.time()-gent_start)
iter_fun()
gent_func()
显示结果的含义:第一列表示已分析代码的行号,第二列(Mem 使用情况)表示执行该行后 Python 解释器的内存使用情况。第三列(增量)表示当前行相对于最后一行的内存差异。最后一列(行内容)打印已分析的代码。
分析:在不进行计算的情况下,列表list和迭代器会占用空间,但对于生成器不会占用空间
当需要计算时,list和生成器的花费时间和占用内存
使用sum内置函数,list和生成器求和10000000个数据,list内存占用较大,生成器花费时间大概是list的两倍
import time
from memory_profiler import profile
@profile(precision=4)
def iter_fun():
start = time.time()
total = sum([i for i in range(10000000)])
print('iter_spend_time:',time.time()-start)
@profile(precision=4)
def gent_func():
gent_start = time.time()
total = sum(i for i in range(10000000))
print('gent_spend_time:',time.time()-gent_start)
iter_fun()
gent_func()
比较分析,如果需要对数据进行迭代使用时,生成器方法的耗时较长,但内存使用方面还是较少,因为使用生成器时,内存只存储每次迭代计算的数据。分析原因时个人认为,生成器的迭代计算过程中,在迭代数据和计算直接不断转换,相比与迭代器对象中先将数据全部保存在内存中(虽然占内存,但读取比再次迭代要快),因此,生成器比较费时间,但占用内存小。
记录数据循环求和500000个数据,迭代器和生成器循环得到时
总结:几乎同时完成,迭代器的占用内存较大
import time
from memory_profiler import profile
itery = iter([i for i in range(5000000)])
gent = (i for i in range(5000000))
@profile(precision=4)
def iter_fun():
start = time.time()
total= 0
for item in itery:
total+=item
print('iter:',time.time()-start)
@profile(precision=4)
def gent_func():
gent_start = time.time()
total = 0
for item in gent:
total+=item
print('gent:',time.time()-gent_start)
iter_fun()
gent_func()
list,迭代器和生成器共同使用sum计算5000000个数据时间比较
总结:list+sum和迭代器+sum计算时长差不多,但生成器+sum计算的时长几乎长一倍,
import time
from memory_profiler import profile
@profile(precision=4)
def list_fun():
start = time.time()
print('start!!!')
list_data = [i for i in range(5000000)]
total = sum(list_data)
print('iter_spend_time:',time.time()-start)
@profile(precision=4)
def iter_fun():
start = time.time()
total = 0
total = sum(iter([i for i in range(5000000)]))
print('total:',total)
print('iter_spend_time:',time.time()-start)
@profile(precision=4)
def gent_func():
gent_start = time.time()
total = sum(i for i in range(5000000))
print('total:',total)
print('gent_spend_time:',time.time()-gent_start)
list_fun()
iter_fun()
gent_func()
来源:https://blog.csdn.net/weixin_43794311/article/details/125462691
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 1.为什么需要创建虚拟环境?虚拟环境是隔离应用确切依赖项的好办法。此类隔离避免了全局 Python 环境中的冲突,有助于进行测试和协作。随着
- python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,坐着的椅子就是对象,家里养的小狗也是一个对象。。。。。。我们通
- 1. 对于数组array乘就是对应位置的元素相乘:X1 = np.array([[1,2], [3, 4]])X2 = X1print X2
- --BEGIN DISTRIBUTED TRANSACTION [transactionname]--标志一个由分布式事务处理协调器MSDT
- 使用tkinter实现下拉多选框效果如图:1、选择一些选项2、全选选项代码如下:import tkinterfrom ComBoPicker
- 昨天带伙伴萌学习python爬虫,准备了几个简单的入门实例涉及主要知识点:web是如何交互的requests库的get、post函数的应用r
- 很早就听说韩国网站的设计师们很会利用空间,来创造更多的信息承载量.最近浏览了几个韩国SHOPPING网站果不其然,就拿小小的广告轮播来说,非
- 如下所示:import json# 使用三引号将浏览器复制出来的requests headers参数赋值给一个变量headers = &qu
- 简介 函数式编程语言在学术领域已经存在相当长一段时间了,但是从历史上看,它们没有丰富的工具和库可供使用。随着 .NET 平台上的
- 块级元素块级元素生成一个元素框,(默认地)它会填充其父级元素的内容,旁边不能有其他元素。换句话说,他在元素框之前和之后生成了“分隔”符。我们
- 对range()逆序的解释在 Python3.6 中,range() 函数返回一个可迭代的范围对象,范围类型表示不可变的数字序列,一般用在
- 由于数据库中的每张表中的字段名不一样,所以在写后台程序的时候,通常会写多个页面,对应不同的表。这里介绍一个函数,适合所有的表,不需要再为每一
- XML的嵌套处理 一般情况下,我们从数据库中查询得到的结果集可能很大,所以从服务器返回到客户端时,数据会被分成若干个页面分别进行传递。此时,
- 题目要求1.后台管理员只有一个用户:admin, 密码: admin2.当管理员登陆成功后,可以管理前台会员信息。3.会员信息管
- 先上效果,视频敬上:字符舞:代码舞源代码:video_2_code_video.pyimport argparseimport osimpo
- 使用 Python 内建的defaultdict 方法可以轻松定义一个树的数据结构。简单的说树也可以是一个字典数据结构 def t
- 在项目中,我们需要运用到很多来自后端返回的数据。有时是上百条,有时甚至上千条。如果加上后端的多表查询或者数据量过大,这就导致在前
- 本篇文章将介绍在 Python 中将 hex 转换为 base64 的方法。使用 Python 中的 codecs 模块将 HEX 转换为
- 首先,让我们介绍一下什么是pytorch,它是一个基于Python的开源深度学习框架,它提供了两个核心功能:张量计算和自动求导。张量计算张量
- (1)序列化即js中的Object转化为字符串1.使用obj.toJSONString()var str=obj.toJSONString(