📜  查找 NumPy 数组的内存大小

📅  最后修改于: 2022-05-13 01:55:30.508000             🧑  作者: Mango

查找 NumPy 数组的内存大小

在这篇文章中,我们将看到如何找到 NumPy 数组的内存大小。因此,为了找到内存大小,我们使用以下方法:

方法一:使用 NumPy 数组的sizeitemize属性。

让我们看看例子:

示例 1:

Python3
# import library
import numpy as np
  
# create a numpy 1d-array
x = np.array([100,20,34])
  
print("Size of the array: ",
      x.size)
  
print("Memory size of one array element in bytes: ",
      x.itemsize)
  
# memory size of numpy array in bytes
print("Memory size of numpy array in bytes:",
      x.size * x.itemsize)


Python3
# import library
import numpy as np
  
# create a numpy 2d-array
x = np.array([[100, 20, 34],
              [300, 400, 600]])
  
print("Size of the array: ",
      x.size)
  
print("Memory size of one array element in bytes: ",
      x.itemsize)
  
# memory size of numpy array
print("Memory size of numpy array in bytes:",
      x.size * x.itemsize)


Python3
# import library
import numpy as np
  
# create numpy 1d-array
x = np.array([100, 20, 34])
  
print("Memory size of a NumPy array:",
      x.nbytes)


Python3
# import library
import numpy as np
  
# create numpy 2d-array
x = np.array([[100, 20, 34],
              [300, 400, 600]])
  
print("Memory size of a NumPy array:",
      x.nbytes)


输出:

Size of the array:  3
Memory size of one array element in bytes:  4
Memory size of numpy array in bytes: 12

示例 2:

Python3

# import library
import numpy as np
  
# create a numpy 2d-array
x = np.array([[100, 20, 34],
              [300, 400, 600]])
  
print("Size of the array: ",
      x.size)
  
print("Memory size of one array element in bytes: ",
      x.itemsize)
  
# memory size of numpy array
print("Memory size of numpy array in bytes:",
      x.size * x.itemsize)

输出:

Size of the array:  6
Length of one array element in bytes:  4
Memory size of numpy array in bytes: 24

方法二:使用 NumPy 数组的nbytes属性。

让我们看看例子:

示例 1:

Python3

# import library
import numpy as np
  
# create numpy 1d-array
x = np.array([100, 20, 34])
  
print("Memory size of a NumPy array:",
      x.nbytes)

输出:

Memory size of a NumPy array: 12

示例 2:

Python3

# import library
import numpy as np
  
# create numpy 2d-array
x = np.array([[100, 20, 34],
              [300, 400, 600]])
  
print("Memory size of a NumPy array:",
      x.nbytes)

输出:

Memory size of a NumPy array: 24