float 32 数据和 float 64 数据的内存使用相同吗?
Same Memory Usage for float 32 data and float 64 data?
我尝试使用 tracemalloc 来跟踪我的编码的内存使用情况,我发现对于 float 32 数据和 float 64 数据,它们具有相同的内存使用峰值。为什么?
输出截图:
float64:
import tracemalloc
import numpy as np
tracemalloc.start()
arr = np.zeros((100000000,), dtype=np.float64)
for i in range(100000000):
arr[i] = i
current, peak = tracemalloc.get_traced_memory()
#current memory is the memory the code is currently using and
#peak memory is the maximum space the program used while executing.
tracemalloc.stop()
print(f"Current memory usage is {current / 10**6}MB; Peak was {peak / 10**6}MB")
输出:
Current memory usage is 800.14981MB; Peak was 800.160145MB
float32:
import tracemalloc
import numpy as np
tracemalloc.start()
arr = np.zeros((100000000,), dtype=np.float32)
arr = arr.astype(np.float32)
for i in range(100000000):
arr[i] = i
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
print(f"Current memory usage is {current / 10**6}MB; Peak was {peak / 10**6}MB")
输出:
Current memory usage is 400.149626MB; Peak was 800.149468MB
您可以看到这 2 个片段并不相同。第二个片段中有这个附加声明
arr = arr.astype(np.float32)
该语句(即使 arr
已经有类型 np.float32
):
- 复制数组(占用与旧数组相同的内存量)
- 然后将新数组赋值给
arr
,
- 然后丢弃旧数组。
因此内存峰值翻倍。
我尝试使用 tracemalloc 来跟踪我的编码的内存使用情况,我发现对于 float 32 数据和 float 64 数据,它们具有相同的内存使用峰值。为什么?
输出截图:
float64:
import tracemalloc
import numpy as np
tracemalloc.start()
arr = np.zeros((100000000,), dtype=np.float64)
for i in range(100000000):
arr[i] = i
current, peak = tracemalloc.get_traced_memory()
#current memory is the memory the code is currently using and
#peak memory is the maximum space the program used while executing.
tracemalloc.stop()
print(f"Current memory usage is {current / 10**6}MB; Peak was {peak / 10**6}MB")
输出:
Current memory usage is 800.14981MB; Peak was 800.160145MB
float32:
import tracemalloc
import numpy as np
tracemalloc.start()
arr = np.zeros((100000000,), dtype=np.float32)
arr = arr.astype(np.float32)
for i in range(100000000):
arr[i] = i
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
print(f"Current memory usage is {current / 10**6}MB; Peak was {peak / 10**6}MB")
输出:
Current memory usage is 400.149626MB; Peak was 800.149468MB
您可以看到这 2 个片段并不相同。第二个片段中有这个附加声明
arr = arr.astype(np.float32)
该语句(即使 arr
已经有类型 np.float32
):
- 复制数组(占用与旧数组相同的内存量)
- 然后将新数组赋值给
arr
, - 然后丢弃旧数组。
因此内存峰值翻倍。