I want to create a small NumPy integer to save memory. However, I noticed that
import numpy,sys
print sys.getsizeof(numpy.int8(1))
prints 12
, so it seems that numpy.int8()
is generating 12 bytes instead of 1 byte of data. Why is this?
Numpy scalars have similar implementations to the CPython data types, such as the python float or integer types. In other words, it is a struct containing three variables:
A reference counter of type int
A pointer to an instance of its object type
The value of the variable
The reference counter and the pointer would usually be 4 bytes each in 32-bit architectures. The value field could be any size in principle, but structure padding will cause the struct to allocate 4 bytes, even if value requires less.
If you are working on a 64-bit architecture, replace "4 bytes" with "8 bytes".
So, all numpy integers on a 32-bit system consist of (effectively) three 4-byte variables, and sys.getsizeof(numpy.int8(1))
gives 12. On a 64-bit system, it would return 24.
Most numpy scalars are the same size. One exception is the numpy.complex
type, which generally requires double storage. Since this aligns in memory with the other two variables, you can ignore padding and simply double the bytes allocated for the value, giving 16 and 32 bytes in 32- and 64-bit, respectively. Similar rules apply to the more exotic types (complex256
, float80
, etc.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With