Python列表(元组)中每个元素有多少个字节? [英] How many bytes per element are there in a Python list (tuple)?

查看:588
本文介绍了Python列表(元组)中每个元素有多少个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,存储一百万个(32位)整数列表需要多少内存?

alist = range(1000000) # or list(range(1000000)) in Python 3.0

解决方案

有用的链接:

如何获取python对象的内存大小/使用情况

python对象的内存大小?

如果将数据放入字典中,我们如何计算数据大小?

但是他们没有给出明确的答案.要走的路:

  1. 在有/没有列表的情况下测量Python解释器消耗的内存(使用OS工具).

  2. 使用第三方扩展模块,该模块定义某种sizeof(PyObject).

更新:

菜单546530:Python对象的大小(已修订)

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:

  1. Measure memory consumed by Python interpreter with/without the list (use OS tools).

  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆