为什么 int 在 Python 中需要三倍的内存?
问题描述
在 64 位系统上,Python 中的整数占用 24 个字节.这是例如所需内存的 3 倍.C 表示 64 位整数.现在,我知道这是因为 Python 整数是对象.但是额外的内存有什么用呢?我有我的猜测,但很高兴知道.
On a 64-bit system an integer in Python takes 24 bytes. This is 3 times the memory that would be needed in e.g. C for a 64-bit integer. Now, I know this is because Python integers are objects. But what is the extra memory used for? I have my guesses, but it would be nice to know for sure.
解决方案
请记住,Python int
类型没有像 C int
那样的有限范围;唯一的限制是可用内存.
Remember that the Python int
type does not have a limited range like C int
has; the only limit is the available memory.
内存用于存储值、整数存储的当前大小(存储大小可变以支持任意大小)和标准 Python 对象簿记(对相关对象的引用和引用计数).
Memory goes to storing the value, the current size of the integer storage (the storage size is variable to support arbitrary sizes), and the standard Python object bookkeeping (a reference to the relevant object and a reference count).
您可以查找 longintrepr.h
source(Python 3 int
类型在 Python 2 中传统上称为 long
类型);它有效地利用了 PyVarObject
C 类型 跟踪整数大小:
You can look up the longintrepr.h
source (the Python 3 int
type was traditionally known as the long
type in Python 2); it makes effective use of the PyVarObject
C type to track integer size:
struct _longobject {
PyObject_VAR_HEAD
digit ob_digit[1];
};
ob_digit
数组存储 15 位或 30 位宽的数字"(取决于您的平台);所以在我的 64 位 OS X 系统上,一个不超过 (2 ^ 30) - 1 的整数使用 1 个数字":
The ob_digit
array stores 'digits' of either 15 or 30 bits wide (depending on your platform); so on my 64-bit OS X system, an integer up to (2 ^ 30) - 1 uses 1 'digit':
>>> sys.getsizeof((1 << 30) - 1)
28
但如果您在数字中使用 2 个 30 位数字,则需要额外的 4 个字节,等等:
but if you use 2 30-bit digits in the number an additional 4 bytes are needed, etc:
>>> sys.getsizeof(1 << 30)
32
>>> sys.getsizeof(1 << 60)
36
>>> sys.getsizeof(1 << 90)
40
基本的 24 字节是 PyObject_VAR_HEAD
结构,保存对象大小、引用计数和类型指针(在我的 64 位 OS X 平台上每个 8 字节/64 位).
The base 24 bytes then are the PyObject_VAR_HEAD
structure, holding the object size, the reference count and the type pointer (each 8 bytes / 64 bits on my 64-bit OS X platform).
在 Python 2 上,整数 <= sys.maxint
但 >= -sys.maxint - 1
使用 更简单的结构只存储单个值:
On Python 2, integers <= sys.maxint
but >= -sys.maxint - 1
are stored using a simpler structure storing just the single value:
typedef struct {
PyObject_HEAD
long ob_ival;
} PyIntObject;
因为这使用 PyObject
而不是 PyVarObject
结构中没有 ob_size
字段并且内存大小仅限于 24 字节;long
值是 8,引用计数是 8,类型对象指针是 8.
because this uses PyObject
instead of PyVarObject
there is no ob_size
field in the struct and the memory size is limited to just 24 bytes; 8 for the long
value, 8 for the reference count and 8 for the type object pointer.
相关文章