0

I just wrote this primitive script:

from sys import getsizeof as g
x = 0
s = ''
while s != 'q':
    x = (x << 8) + 0xff
    print(str(x) + "    [" + str(g(x)) + "]")
    s = input("Enter to proceed, 'q' to quit  ")

The output is as follows - and quite surprising, as I perceive it:

255                    [28]
65535                  [28]
16777215               [28]
4294967295             [32]
1099511627775          [32]
281474976710655        [32]
72057594037927935      [32]
18446744073709551615   [36]

And so on. My point is: it seems that the variable x has some sort of 'overhead' with a size of 25 bytes. Where does this come from? Thanks in advance for any attempt to help me.

1
  • Everything is an object in Python. Commented Jul 11, 2017 at 18:39

1 Answer 1

1

A python int is an object, so it's not surprising that it has a small overhead. If this overhead starts to become meaningful for you then this implies you're manipulating substantial collections of ints, which suggests to me that the numpy library is probably something you should consider.

Sign up to request clarification or add additional context in comments.

3 Comments

Or even array.array, or struct
@juanpa.arrivillaga Yes, those are good for the raw representation, but numpy brings a lot of tools for manipulation.
Yep, I agree. But sometimes, those alternatives are all you need, and they are part of the standard library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.