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.
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?
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.
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?
Unless you're dealing with an array.array or a numpy.array - the size always has object overhead. And since Python deals with BigInts naturally, it's really, really hard to tell...
>>> 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.
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 object rather than raw storage...
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
python - "sys.getsizeof(int)" returns an unreasonably large value? - Stack Overflow
computer science - Python variable in bytes - Software Engineering Stack Exchange
What exactly is in the 32 bytes to represent an integer?
Why is a boolean variable 24 bytes in python?
Going through the docs I didn't find a definite answer
If I do
x = 234234 sys.getsizeof(x)
It returns 28, so that's 224 bits ?
Apparently getsizeof() returns the size of an object in bytes, is this size consistent across all int's in python though?
I know that there are long ints, and that they are converted automatically to long ints at a certain value in python, but I'm just wondering about ints for now.
In Java the size that I got was 32 bit, so 4 bytes.
Is this correct, that :
python int = 224 bits java int = 32 bits
And is this because the python int has more methods etc?
Basically what I was trying to state was that across programming languages the data type sizes were fairly conventional and that an int in C# would be the same size as an int in Python / Java. I think this might be wrong though.
Thanks!
According pympler's asizeof, the integer 1 takes 32 bytes of storage. Can anyone say in detail what is in those 32 bytes?