I am trying to use numpy
‘s nbytes
attribute to examine the memory usage of arrays with different dtype
. I noticed the following:
>>> np.zeros(1024, dtype='int64').nbytes / 1024 # in kB
8.0
>>> np.zeros(1024, dtype='int8').nbytes / 1024
1.0
>>> np.zeros(1024, dtype='bool').nbytes / 1024
1.0 # ?
The results of the first two lines make sense to me, since the memory usage reported (8 kB and 1 kB, resp.) is simply the sum of the memory usage of each element (8 and 1 bytes, resp.) multiplied by the number of elements (1024 in both cases). However, in the last case, I would assume that an array of bool
type only requires one bit of memory per element, however in seems that it requires 8 bits per element, same as int8
.
My questions:
- Am I interpreting correctly the output of
nbytes
? If so, how should we understand the fact that abool
type does not use one bit per element? - With this in mind, is there a reason to use a
bool
type array over anint8
?
This question came up as I am dealing with arrays of binary (0 and 1) values, and was trying to find the space efficient solution (in terms of memory, since I assume there are not differences in computation times?), which I thought would be using a bool
type.