Python列表(元组)中每个元素有多少个字节? [英] How many bytes per element are there in a Python list (tuple)?
本文介绍了Python列表(元组)中每个元素有多少个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,存储一百万个(32位)整数列表需要多少内存?
alist = range(1000000) # or list(range(1000000)) in Python 3.0
解决方案
有用的链接:
但是他们没有给出明确的答案.要走的路:
-
在有/没有列表的情况下测量Python解释器消耗的内存(使用OS工具).
-
使用第三方扩展模块,该模块定义某种sizeof(PyObject).
更新:
import asizeof
N = 1000000
print asizeof.asizeof(range(N)) / N
# -> 20 (python 2.5, WinXP, 32-bit Linux)
# -> 33 (64-bit Linux)
For example, how much memory is required to store a list of one million (32-bit) integers?
alist = range(1000000) # or list(range(1000000)) in Python 3.0
解决方案
Useful links:
How to get memory size/usage of python object
Memory sizes of python objects?
if you put data into dictionary, how do we calculate the data size?
However they don't give a definitive answer. The way to go:
Measure memory consumed by Python interpreter with/without the list (use OS tools).
Use a third-party extension module which defines some sort of sizeof(PyObject).
Update:
Recipe 546530: Size of Python objects (revised)
import asizeof
N = 1000000
print asizeof.asizeof(range(N)) / N
# -> 20 (python 2.5, WinXP, 32-bit Linux)
# -> 33 (64-bit Linux)
这篇关于Python列表(元组)中每个元素有多少个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文