파이썬에서 같은 수의 요소를 가진 리스트와 넘파이 배열의 크기가 어떻게 다른지 궁금하여 알아보았다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import sys
import numpy as np
 
array_1 = [x for x in range(64)]
array_2 = [[x for x in range(8)] for y in range(8)]
array_3 = [[[x for x in range(4)] for y in range(4)] for z in range(4)]
 
np_array_1 = np.array(array_1)
np_array_2 = np.array(array_2)
np_array_3 = np.array(array_3)
 
print("[sys.getsizeof] array_1: ",sys.getsizeof(array_1))
print("[sys.getsizeof] array_2: ",sys.getsizeof(array_2))
print("[sys.getsizeof] array_3: ",sys.getsizeof(array_3),"\n")
 
print("[sys.getsizeof] np_array_1: ",sys.getsizeof(np_array_1))
print("[sys.getsizeof] np_array_2: ",sys.getsizeof(np_array_2))
print("[sys.getsizeof] np_array_3: ",sys.getsizeof(np_array_3),"\n")
 
print("[ndarray.nbytes] np_array_1: ", np_array_1.nbytes)
print("[ndarray.nbytes] np_array_2: ", np_array_2.nbytes)
print("[ndarray.nbytes] np_array_3: ", np_array_3.nbytes)
cs

 

 

[출력 결과]

 

sys.getsizeof: Return the size of an object in bytes.

객체의 메모리 사이즈를 바이트 단위로 출력

 

numpy.ndarray.nbytes: Total bytes consumed by the elements of the array.

배열의 요소들에 의해 사용된 전체 바이트 수

 


[sys.getsizeof] array_1~3

[sys.getsizeof] np_array_1~3


[ndarray.nbytes] np_array_1~3

 

 

 

nbytes는 저장된 데이터만을 반환하고 sys.getsizeof는 전체 객체 크기를 반환합니다.

 

참고

https://stackoverflow.com/questions/35421869/nbytes-and-getsizeof-return-different-values

 

nbytes and getsizeof return different values

I have noticed that nbytes and getsizeof returns two different values when the bank to a NumPy array. Example: import sys import numpy as np x = np.random.rand(10000, 50) print('x.nbytes: {0} byt...

stackoverflow.com

 

 

https://docs.python.org/3/library/sys.html

 

sys — System-specific parameters and functions — Python 3.10.2 documentation

sys — System-specific parameters and functions This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available. sys.abiflags On POSIX systems where P

docs.python.org

https://numpy.org/doc/stable/reference/generated/numpy.ndarray.nbytes.html

 

numpy.ndarray.nbytes — NumPy v1.22 Manual

 

numpy.org

 

 

'기타' 카테고리의 다른 글

GrayScale변환 후 출력이 다르게 나오는 이유  (0) 2021.08.11

+ Recent posts