OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

NumPy array size for int8 vs bool dtype

  • Thread starter Thread starter Kalo
  • Start date Start date
K

Kalo

Guest
I am trying to use numpy's nbytes attribute to examine the memory usage of arrays with different dtype. I noticed the following:

Code:
>>> 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 a bool type does not use one bit per element?
  • With this in mind, is there a reason to use a bool type array over an int8?


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.
<p>I am trying to use <code>numpy</code>'s <code>nbytes</code> attribute to examine the memory usage of arrays with different <code>dtype</code>. I noticed the following:</p>
<pre class="lang-py prettyprint-override"><code>>>> 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 # ?
</code></pre>
<p>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 <code>bool</code> type only requires one bit of memory per element, however in seems that it requires 8 bits per element, same as <code>int8</code>.</p>
<p>My questions:</p>
<ul>
<li>Am I interpreting correctly the output of <code>nbytes</code>? If so, how should we understand the fact that a <code>bool</code> type does not use one bit per element?</li>
<li>With this in mind, is there a reason to use a <code>bool</code> type array over an <code>int8</code>?</li>
</ul>
<hr />
<p>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 <code>bool</code> type.</p>
 

Latest posts

H
Replies
0
Views
1
habrewning
H
Top