Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python memory consumption in 64 bit system for int and float

I'm trying the below code in a 64 bit system on Python 3.4 to understand the memory consumption of different primitive data types.

import sys
print(sys.getsizeof(45)) # prints 28
print(sys.getsizeof(45.2)) # prints 24

My question is why Integer takes more space than the float value. But on the contrary , In a 32 bit system

import sys
print(sys.getsizeof(45)) # prints 14
print(sys.getsizeof(45.2)) # prints 16

Integer takes less memory than the float. Why is this behavior? Is it depends upon the Operating System as well other than the Chip set size ?

like image 947
RamValli Avatar asked Jul 17 '15 06:07

RamValli


1 Answers

The overheads(PyObject_HEAD) have doubled, but while the size of ints goes from 32 to 64 bits, the size of floats (doubles) remains 64

32 bit
int:   overhead = 10 bytes, value = 4 bytes
float: overhead =  8 bytes, value = 8 bytes

64 bit
int:   overhead = 20 bytes, value = 8 bytes
float: overhead = 16 bytes, value = 8 bytes

Note that int can be larger than this if they can't fit in the native datatype

like image 93
John La Rooy Avatar answered Oct 31 '22 18:10

John La Rooy