Reputation: 41
I have tried to use tracemalloc to trace the memory usage of my coding and I found that for float 32 data and float 64 data, they have same peak memory usage. Why?
Screenshot of output:
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")
Output:
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")
Output:
Current memory usage is 400.149626MB; Peak was 800.149468MB
Upvotes: 1
Views: 1422
Reputation: 3955
You can see that the 2 snippets are not equivalent. There's this additional statement in the second snippet
arr = arr.astype(np.float32)
That statement (even though arr
already have type np.float32
):
arr
,Therefore the peak memory doubles.
Upvotes: 2