所以我想看2种算法的内存消耗?我的计划是在算法之前、算法之后获取内存消耗,然后减去结果。
before = tracemalloc.get_traced_memory()[0]
algorithm()
after = tracemalloc.get_traced_memory()[0]
print(after - before)
所以我试了一下:
import tracemalloc
tracemalloc.start()
before1 = tracemalloc.get_traced_memory()[0]
def recursive(i,max):
i = i+1
if(i>max): return
recursive(i,max)
recursive(0,5)
after1 = tracemalloc.get_traced_memory()[0]
tracemalloc.stop()
print(after1- before1)
但无论出于何种原因,输出总是在 400 到 2000 之间变化?我并没有真正落后。谢谢 :)