Python Practice › NumPy Programs › Program 13

Memory used by an array

EasyNumPy Programs · 13 of 100

Write a NumPy program to find the memory occupied by an array.

Advertisement
Output
5 8 40

Approach

  1. nbytes = number of items × bytes per item.

Solution

Python
import numpy as nparr = np.array([10, 34, 86, 26, 56], dtype=np.int64)print(arr.size, arr.itemsize, arr.nbytes)
Output
5 8 40

Line by line

Line 2

The dtype is fixed to int64 so the size is the same on every platform.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs