Does anybody know how much memory is used by a numpy ndarray? (with let's say 10,000,000 float elements).
The size in memory of numpy arrays is easy to calculate. It's simply the number of elements times the data size, plus a small constant overhead. For example, if your cube. dtype is int64 , and it has 1,000,000 elements, it will require 1000000 * 64 / 8 = 8,000,000 bytes (8Mb).
In NumPy array, all the elements have the same data type. itemsize returns the size (in bytes) of each element of a NumPy array. e.g. for this NumPy array [ [3,4,6], [0,8,1]], itemsize will be 8, because this array consists of integers and size of integer (in bytes) is 8 bytes.
1. NumPy uses much less memory to store data. The NumPy arrays takes significantly less amount of memory as compared to python lists. It also provides a mechanism of specifying the data types of the contents, which allows further optimisation of the code.
There are two main reasons why we would use NumPy array instead of lists in Python. These reasons are: Less memory usage: The Python NumPy array consumes less memory than lists.
The array is simply stored in one consecutive block in memory. Assuming by "float" you mean standard double precision floating point numbers, then the array will need 8 bytes per element.
In general, you can simply query the nbytes
attribute for the total memory requirement of an array, and itemsize
for the size of a single element in bytes:
>>> a = numpy.arange(1000.0) >>> a.nbytes 8000 >>> a.itemsize 8
In addtion to the actual array data, there will also be a small data structure containing the meta-information on the array. Especially for large arrays, the size of this data structure is negligible.
To get the total memory footprint of the NumPy array in bytes, including the metadata, you can use Python's sys.getsizeof()
function:
import sys import numpy as np a = np.arange(1000.0) sys.getsizeof(a)
8104 bytes is the result
sys.getsizeof()
works for any Python object. It reports the internal memory allocation, not necessarily the memory footprint of the object once it is written out to some file format. Sometimes it is wildly misleading. For example, with 2d arrays, it doesn't dig into the memory footprint of vector.
See the docs here. Ned Batcheldor shares caveats with using sys.getsizeof()
here.
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