python list内存_python - list()比列表理解使用更多的内存

我认为你看到过度分配模式这是来自源的样本:

/* This over-allocates proportional to the list size, making room

* for additional growth. The over-allocation is mild, but is

* enough to give linear-time amortized behavior over a long

* sequence of appends() in the presence of a poorly-performing

* system realloc().

* The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...

*/

new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);

打印长度为0-88的列表推导的大小,您可以看到模式匹配:

# create comprehensions for sizes 0-88

comprehensions = [sys.getsizeof([1 for _ in range(l)]) for l in range(90)]

# only take those that resulted in growth compared to previous length

steps = zip(comprehensions, comprehensions[1:])

growths = [x for x in list(enumerate(steps)) if x[1][0] != x[1][1]]

# print the results:

for growth in growths:

print(growth)

结果(格式为list.resize):

(0, (64, 96))

(4, (96, 128))

(8, (128, 192))

(16, (192, 264))

(25, (264, 344))

(35, (344, 432))

(46, (432, 528))

(58, (528, 640))

(72, (640, 768))

(88, (768, 912))

过度分配是出于性能原因而完成的,允许列表增长,而不会在每次增长时分配更多内存(更好的摊销性能)。

使用列表理解存在差异的可能原因是列表理解不能确定性地计算生成列表的大小,但是list.resize可以。 这意味着理解将不断增加列表,因为它使用过度分配填充它,直到最终填充它。

一旦完成,有可能不会使用未使用的已分配节点增加过度分配缓冲区(实际上,在大多数情况下它不会,这会破坏过度分配的目的)。

但是,list.resize可以添加一些缓冲区,无论列表大小如何,因为它事先知道最终列表大小。

同样来自消息来源的另一个支持证据是,我们看到列表推导调用list.resize,其表示使用list.resize,这反过来表示消耗预分配缓冲区而不知道将填充多少。 这与您所看到的行为一致。

总之,list()将根据列表大小预先分配更多节点

>>> sys.getsizeof(list([1,2,3]))

60

>>> sys.getsizeof(list([1,2,3,4]))

64

列表理解不知道列表大小,因此它在增长时使用追加操作,耗尽预分配缓冲区:

# one item before filling pre-allocation buffer completely

>>> sys.getsizeof([i for i in [1,2,3]])

52

# fills pre-allocation buffer completely

# note that size did not change, we still have buffered unused nodes

>>> sys.getsizeof([i for i in [1,2,3,4]])

52

# grows pre-allocation buffer

>>> sys.getsizeof([i for i in [1,2,3,4,5]])

68

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python程序的内存使用情况可以使用Python内置的`memory_profiler`模块进行监视。`memory_profiler`可以帮助我们分析Python程序的内存使用情况,找出内存泄漏等问题。 安装`memory_profiler`模块: ```python pip install memory_profiler ``` 使用`memory_profiler`监视Python程序的内存使用情况: 1. 在需要监视的函数上添加`@profile`装饰器。 2. 运行程序时加上`-m memory_profiler`参数,并指定要监视的文件名。 例如,我们有以下Python程序: ```python from random import randint @profile def generate_list(): nums = [randint(0, 100) for _ in range(1000000)] return nums if __name__ == '__main__': nums = generate_list() print(sum(nums)) ``` 我们可以在`generate_list()`函数上加上`@profile`装饰器,然后运行以下命令: ```python python -m memory_profiler memory_test.py ``` 输出结果: ``` Filename: memory_test.py Line # Mem usage Increment Line Contents ================================================ 3 52.5 MiB 52.5 MiB @profile 4 def generate_list(): 5 81.9 MiB 29.4 MiB nums = [randint(0, 100) for _ in range(1000000)] 6 81.9 MiB 0.0 MiB return nums 12671490 ``` 输出结果中,`Line #`表示代码行号,`Mem usage`表示该行执行后的内存使用情况,`Increment`表示相对于上一行的内存增加量,`Line Contents`表示该行代码内容。我们可以看到,在`generate_list()`函数中,内存使用量从52.5 MiB增加到81.9 MiB,增加了29.4 MiB,随着程序的执行结束,内存使用量又降回到了52.5 MiB。 通过`memory_profiler`模块的输出结果,我们可以找出Python程序中的内存泄漏和不必要的内存占用,从而进行优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值