Python中变量的内存大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14372006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:13:27  来源:igfitidea点击:

Variable's memory size in Python

pythonvariablesmemory

提问by user4478

I am writing Python code to do some big number calculation, and have serious concern about the memory used in the calculation.

我正在编写 Python 代码来进行一些大数字计算,并且非常关注计算中使用的内存。

Thus, I want to count every bit of each variable.

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

For example, I have a variable x, which is a big number, and want to count the number of bits for representing x.

例如,我有一个变量x,它是一个很大的数字,并且想要计算表示x的位数。

The following code is obviously useless:

下面的代码显然没用:

x=2**1000
len(x)

Thus, I turn to use the following code:

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

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

The variable xis (in decimal) is:

变量x是(十进制)是:

10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

but the above code returns 303

但上面的代码返回303

The above long long sequence is of length 302, and so I believe that 303should be related to the string length only.

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

So, here comes my original question:

所以,这是我原来的问题:

How can I know the memory size of variable x?

我怎么知道变量x的内存大小?

One more thing; in C/C++ language, if I define

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

int z=1;

This means that there are 4 bytes= 32 bits allocated for z, and the bits are arranged as 00..001(31 0's and one 1).

这意味着为z分配了 4 个字节 = 32 位,并且这些位排列为 00..001(31 个 0 和一个 1)。

Here, my variable xis huge, I don't know whether it follows the same memory allocation rule?

这里,我的变量x很大,不知道是不是遵循同样的内存分配规则?

采纳答案by Jonathon Reinhart

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

使用sys.getsizeof得到一个对象的大小,以字节为单位。

>>> 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.

请注意,对象的大小和布局纯粹是特定于实现的。例如,CPython 可能使用与 IronPython 完全不同的内部数据结构。因此,对象的大小可能因实现而异。

回答by casevh

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

关于 Python long 的内部结构,请检查 sys.int_info(或 Python 2.7 的 sys.long_info)。

>>> 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

Python 要么将 30 位存储为 4 个字节(大多数 64 位系统),要么将 15 位存储为 2 个字节(大多数 32 位系统)。将实际内存使用量与计算值进行比较,我得到

>>> 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.

由于没有存储任何位,因此 0 有 24 个字节的开销。较大值的内存要求与计算值相匹配。

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

如果您的数字太大以至于您担心 6.25% 未使用的位,您可能应该查看gmpy2库。内部表示使用所有可用位,并且对于大值(例如,大于 100 位)的计算速度要快得多。