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

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

问题描述

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

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

解决方案

有用的链接:

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

python 对象的内存大小?>

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

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

  1. 使用/不使用列表测量 Python 解释器消耗的内存(使用操作系统工具).

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

更新:

配方 546530:Python 对象的大小(已修订)

import asizeofN = 1000000打印 asizeof.asizeof(range(N))/N# ->20(python 2.5、WinXP、32 位 Linux)# ->33(64 位 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天全站免登陆