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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
intA 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.complextype, 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.)