Python 中变量的内存大小

我正在编写 Python 代码来进行一些大量的计算,并对计算中使用的内存表示严重关切。

因此,我要计算每个变量的每个位。

例如,我有一个变量 X,它是一个很大的数字,我想要计算代表 X的位的数目。

下面的代码显然是无用的:

x=2**1000
len(x)

因此,我转而使用以下代码:

x=2**1000
len(repr(x))

变量 X(十进制)是:

5086071862673209484250490600018105614048117055360743750388370351051124936122493198378156958127594672917553146825187145285692314043598457757457469857469857480393456677774674842423098542107460542107460506237141876054182182153046474983581941267377777659165555439466654243868372766868693767676767676767

但是上面的代码返回 303

上面的长长序列的长度是302,所以我认为 303应该只与字符串长度有关。

所以,这是我最初的问题:

如何知道变量 X的内存大小?

还有一件事,在 C/C + + 语言中,如果我定义

int z=1;

这意味着为 Z分配了4个字节 = 32个位,这些位被排列为00。.001(310和1)。

这里,我的变量 X很大,我不知道它是否遵循相同的内存分配规则?

172618 次浏览

Use sys.getsizeof to get the size of an object, in bytes.

>>> from sys import getsizeof
>>> a = 42
>>> getsizeof(a)
12
>>> a = 2**1000
>>> getsizeof(a)
146
>>>

Note that the size and layout of an object is purely implementation-specific. CPython, for example, may use totally different internal data structures than IronPython. So the size of an object may vary from implementation to implementation.

Regarding the internal structure of a Python long, check sys.int_info (or sys.long_info for Python 2.7).

>>> import sys
>>> sys.int_info
sys.int_info(bits_per_digit=30, sizeof_digit=4)

Python either stores 30 bits into 4 bytes (most 64-bit systems) or 15 bits into 2 bytes (most 32-bit systems). Comparing the actual memory usage with calculated values, I get

>>> import math, sys
>>> a=0
>>> sys.getsizeof(a)
24
>>> a=2**100
>>> sys.getsizeof(a)
40
>>> a=2**1000
>>> sys.getsizeof(a)
160
>>> 24+4*math.ceil(100/30)
40
>>> 24+4*math.ceil(1000/30)
160

There are 24 bytes of overhead for 0 since no bits are stored. The memory requirements for larger values matches the calculated values.

If your numbers are so large that you are concerned about the 6.25% unused bits, you should probably look at the gmpy2 library. The internal representation uses all available bits and computations are significantly faster for large values (say, greater than 100 digits).