In this python numpy program, we will determine the size of the memory occupied by the array using NumPy.
Steps to solve the program
- Import the numpy library as np.
- Create an array using np.array().
- Get the size of the array using size.
- Get the memory occupied by the one element in the array in bytes using itemsize.
- Get the memory size of NumPy array in bytes using x.size * x.itemsize.
- Print the output.
import numpy as np
x = np.array([10,34,86,26,56])
print("Size of the array: ",x.size)
print("Memory size of one array element in bytes: ", x.itemsize)
print("Memory size of numpy array in bytes:", x.size * x.itemsize)
Output :
Size of the array: 5
Memory size of one array element in bytes: 4
Memory size of numpy array in bytes: 20