gpt4 book ai didi

python - python 中的大型对称列表 : will references contribute to size in ram?

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:18 25 4
gpt4 key购买 nike

我有一个非常大的 python 二维列表 L,它是对称的,即 L[x][y]==L[y][x] .它包含整数元组。我创建它的方式是我首先设置 L[x][y] 的值对于所有可能的 x 和 y,使得 x<=y。然后我设置 L[x][y]=L[y][x]对于所有满足 x>y 的 x 和 y,这样我就不必一直检查一对 [x][y] 是否有 x>=y 或 xL[x][y]。

此列表的大小如何受到影响?我的逻辑是因为 L[x][y]因为 x>y 包含一个引用而不是一个元组,那么它不应该在 RAM 中占用那么多空间。是这样吗?如果我只是设置 L[x][y]仅适用于 x<=y ,我会得到大约一半大小的列表吗?

最佳答案

简短的回答 - 是的。但为了证明这一点,让我们举两个例子并使用 memory_profiler .

版本 1,无引用

def build_symmetric_list(n) :
l = []
for x in range(0,n) :
ll = []
for y in range(0,n) :
if x<= y:
ll.append((x,y))
else :
ll.append((y,x))
l.append(ll)
return l

@profile
def test_a():
a = build_symmetric_list(1000)

test_a()

-

Line #    Mem usage    Increment   Line Contents
13 9.848 MiB 0.000 MiB @profile
14 def test_a():
15 106.941 MiB 97.094 MiB a = build_symmetric_list(1000)

第 2 版,带引用文献

def build_symmetric_list_with_references(n) :
l = []
for x in range(0,n) :
ll = []
for y in range(0,n) :
if x<= y :
ll.append((x,y))
else :
ll.append(l[y][x]) #by reference
l.append(ll)
return l

@profile
def test_b() :
b = build_symmetric_list_with_references(1000)
test_b()

结果:

Line #    Mem usage    Increment   Line Contents
================================================
13 9.848 MiB 0.000 MiB @profile
14 def test_b() :
15 65.047 MiB 55.199 MiB b = build_symmetric_list_with_references(1000)

所以 97 MB 与 55 MB - 大约一半。请记住,由于元组是不可变的,因此对 L[x][y] 的重新分配不会反射(reflect)在 L[y][x] 中(可能会使列表的对称性无效)。

关于python - python 中的大型对称列表 : will references contribute to size in ram?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25735950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com