我需要一个非常大的列表,并且正在尝试弄清楚我可以将其设置为多大,以便它仍然适合 1-2GB 的 RAM。我正在使用 64 位 (x86_64) 上的 CPython 实现。
编辑:感谢 bua 的回答,我已经填写了一些更具体的答案。
空间(内存)使用情况是多少(以字节为单位):
- the list itself
- each list entry (not including the data)
-
sys.getsizeof([0, 1, 2, 3]) == 104
,因此每个条目有 8 个字节的开销。
- the data if it is an integer
-
sys.getsizeof(2**62) == 24
(但根据整数大小而变化)
sys.getsizeof(2**63) == 40
sys.getsizeof(2**128) == 48
sys.getsizeof(2**256) == 66
- the data if it is an object (
sizeof(Pyobject)
I guess))
-
sys.getsizeof(C()) == 72
(C 是一个空的用户空间对象)
如果您可以分享有关观察到的尺寸的更多一般数据,那就太好了。例如:
- 是否有特殊情况(我认为不可变值可能是共享的,所以布尔列表可能不会占用任何额外的数据空间)?
- 也许小列表需要 X 字节开销,但大列表需要 Y 字节开销?
开始点:
>>> import sys
>>> a=list()
>>> type(a)
<type 'list'>
>>> sys.getsizeof(a)
36
>>> b=1
>>> type(b)
<type 'int'>
>>> sys.getsizeof(b)
12
以及来自 python 帮助:
>>> help(sys.getsizeof)
Help on built-in function getsizeof in module sys:
getsizeof(...)
getsizeof(object, default) -> int
Return the size of object in bytes.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)