NumPy: Get the memory usage by NumPy arrays
NumPy: Array Object Exercise-112 with Solution
Calculate Memory Usage of Array
Write a NumPy program to get NumPy array memory usage.
Sample Solution:
Python Code:
# Importing the NumPy library and aliasing it as 'np'
import numpy as np
# Importing the getsizeof function from sys module
from sys import getsizeof
# Creating a list 'x' containing 1024 zeros
x = [0] * 1024
# Creating a NumPy array 'y' from the list 'x'
y = np.array(x)
# Printing the size of the list 'x' using the getsizeof() function from the sys module
print(getsizeof(x))
Sample Output:
8248
Explanation:
In the above code –
x = [0] * 1024: Create a Python list 'x' with 1024 elements, each element being 0.
y = np.array(x): Convert the Python list 'x' into a NumPy array 'y'.
getsizeof(x): Use the getsizeof() function from the sys module to get the memory size of the Python list 'x' in bytes.
Finally print() function prints the memory size of the Python list 'x' in bytes.
Python-Numpy Code Editor:
Previous: Write a NumPy program to create a Cartesian product of two arrays into single array of 2D points.
Next: Write a NumPy program to build an array of all combinations of three NumPy arrays.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-112.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics