0
我正在嘗試記錄每個函數的內存消耗。我現在用的是memory_profiler包,我也得到我的總內存消耗最後的圖,但不是每個功能在this nice tutorialPython內存分析器繪圖
我使用下面的腳本test.py
import time
from memory_profiler import profile
#-------------------------------
@profile
def test1():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a
#-------------------------------
@profile
def test2():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a
#-------------------------------
def main():
l1 = []
l2 = []
for i in range (5) :
print "Iteration", i
l1.append(test1())
l2.append(test2())
time.sleep(1)
if i == 2 :
del l1
l1 = []
#-------------------------------
if __name__ == "__main__":
main()
運行它描述通過
mprof run test.py
我得到一些不錯的輸出像
Line # Mem usage Increment Line Contents
================================================
11 46.2 MiB 0.0 MiB @profile
12 def test2():
13 53.8 MiB 7.6 MiB a = [1] * (10 ** 6)
14 206.7 MiB 152.9 MiB b = [2] * (2 * 10 ** 7)
15 53.8 MiB -152.9 MiB del b
16 53.8 MiB 0.0 MiB return a
但是經由
mprof plot
的繪製它只顯示了總的內存消耗,而不是這些漂亮brakets,示出了與鏈路描述的不同功能的執行。有誰知道如何啓用它?
但隨後的'@ profile'標籤沒有被定義'回溯(最近通話最後一個): 文件 「test.py」 ,第4行,在 @profile NameError:name'profile'is not defined' –
HeXor
如果您使用python3,請嘗試'mprof run --python python3 test.py' – Lukic
我在python2.7 – HeXor