0

I am unable to understand the actual memory space that is allocated to integers and floats in python.

From what I know, sizes of int and float variables by default in python are 32-bit and 64-bit respectively.

But from results of the following code, it looks like 28-bits and 24-bits are allocated.

i = 2
print(sys.getsizeof(i))

Output: 28

i = 0.02
print(sys.getsizeof(i))

Output: 24

Please let me know what part have I misunderstood here.

  • Getsizeof returns the number of bytes, not bits. And they are full objects, not just a simple chunk of data. – Mark Ransom Feb 16 '23 at 21:35
  • 1
    see also: https://stackoverflow.com/questions/49318826/getting-size-of-primitive-data-types-in-python – slothrop Feb 16 '23 at 21:36

1 Answers1

0

getsizeof actually returns you the amount of bytes of an object - in Python both floats, ints and every other object is a full fledged object: there are no "naked" CPU primitives.

That account for most of the performance hits Python has when compared with similar mixed bytecode-interpreted languages, when benchmarking.

That said, Python floats are, indeed, internally 64 bit floats, and Python INTs are special unlimited objects which are stored internally in base-256 and are variable length featuring arbitrary precision

jsbueno
  • 99,910
  • 10
  • 151
  • 209