在 Python 中获取整数的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14329794/
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
Get size of integer in Python
提问by stdcerr
How can I find out the number of Bytes a certain number takes up to store e.g. for \x00 - \xFF I'm looking to get 1 (Byte), \x100 - \xffff would give me 2 (Bytes) and so on. Any clue?
我怎样才能找出某个数字需要存储的字节数,例如对于 \x00 - \xFF 我希望得到 1(字节),\x100 - \xffff 会给我 2(字节)等等。有什么线索吗?
采纳答案by stranac
You can use simple math:
您可以使用简单的数学:
>>> from math import log
>>> def bytes_needed(n):
... if n == 0:
... return 1
... return int(log(n, 256)) + 1
...
>>> bytes_needed(0x01)
1
>>> bytes_needed(0x100)
2
>>> bytes_needed(0x10000)
3
回答by Serdalis
By using a simple biwise operation to move all the used bits over 1 byte each time you can see how many bytes are needed to store a number.
通过使用简单的双向运算将所有使用的位每次移动 1 个字节,您可以看到存储一个数字需要多少个字节。
It's probably worth noting that while this method is very generic, it will not work on negative numbers and only looks at the binary of the variable without taking into account what it is stored in.
可能值得注意的是,虽然这种方法非常通用,但它不适用于负数,并且只查看变量的二进制而不考虑它存储的内容。
a = 256
i = 0
while(a > 0):
a = a >> 8;
i += 1;
print (i)
The program behaves as follows:
该程序的行为如下:
a is 0000 0001 0000 0000in binary
each run of the loop will shift this to the left by 8:
a 是0000 0001 0000 0000二进制的,每次循环运行都会将其向左移动 8:
loop 1:
0000 0001 >> 0000 0000
0000 0001 > 0 (1 > 0)
loop 2:
0000 0000 >> 0000 0001
0000 0000 > 0 (0 > 0)
END 0 is not > 0
so there are 2 bytes needed to store the number.
所以需要 2 个字节来存储数字。
回答by Jon Clements
Unless you're dealing with an array.arrayor a numpy.array- the size always has object overhead. And since Python deals with BigInts naturally, it's really, really hard to tell...
除非您正在处理 anarray.array或 a numpy.array- 大小总是有对象开销。而且由于 Python 自然地处理 BigInts,所以真的很难说......
>>> i = 5
>>> import sys
>>> sys.getsizeof(i)
24
So on a 64bit platform it requires 24 bytes to store what could be stored in 3 bits.
因此,在 64 位平台上,它需要 24 个字节来存储可以存储在 3 位中的内容。
However, if you did,
然而,如果你这样做了,
>>> s = '\x05'
>>> sys.getsizeof(s)
38
So no, not really - you've got the memory-overhead of the definition of the objectrather than raw storage...
所以不,不是真的 - 你有定义object而不是原始存储的内存开销......
If you then take:
如果你然后采取:
>>> a = array.array('i', [3])
>>> a
array('i', [3])
>>> sys.getsizeof(a)
60L
>>> a = array.array('i', [3, 4, 5])
>>> sys.getsizeof(a)
68L
Then you get what would be called normal byte boundaries, etc.. etc... etc...
然后你会得到所谓的正常字节边界,等等......等等......
If you just want what "purely" should be stored - minus object overhead, then from 2.(6|7) you can use some_int.bit_length()(otherwise just bitshift it as other answers have shown) and then work from there
如果您只想存储“纯粹”的内容 - 减去对象开销,那么从 2.(6|7) 开始,您可以使用some_int.bit_length()(否则只需按照其他答案所示对其进行位移),然后从那里开始工作
回答by abarnert
def byte_length(i):
return (i.bit_length() + 7) // 8
Of course, as Jon Clements points out, this isn't the size of the actual PyIntObject, which has a PyObject header, and stores the value as a bignum in whatever way is easiest to deal with rather than most compact, and which you have to have at least one pointer (4 or 8 bytes) to on top of the actual object, and so on.
当然,正如 Jon Clements 指出的,这不是实际 的大小PyIntObject,它有一个 PyObject 标头,并以最容易处理而不是最紧凑的任何方式将值存储为 bignum,并且您必须这样做至少有一个指针(4 或 8 个字节)指向实际对象的顶部,依此类推。
But this is the byte length of the number itself. It's almost certainly the most efficient answer, and probably also the easiest to read.
但这是数字本身的字节长度。这几乎可以肯定是最有效的答案,也可能是最容易阅读的答案。
Or is ceil(i.bit_length() / 8.0)more readable?
或者ceil(i.bit_length() / 8.0)更具可读性?
回答by RICHA AGGARWAL
on python command prompt, you can use size of function
在 python 命令提示符下,您可以使用函数的大小
**$ import python
$ import ctypes
$ ctypes.sizeof(ctypes.c_int)**
回答by The Demz
# Python 3
import math
nbr = 0xff # 255 defined in hexadecimal
nbr = "{0:b}".format(nbr) # Transform the number into a string formated as bytes.
bit_length = len(nbr) # Number of characters
byte_length = math.ceil( bit_length/8 ) # Get minimum number of bytes

