首先,如果我是你,我會通過封裝您在功能提到的那些電話,並呼籲
import cProfile
cProfile.run("my_function()")
後分析我的代碼,例如,我是把重點放在那些小優化,例如您請求在後(!而不是側重於真正的性能豬),我會替換代碼上面:
some_list = [x for x in range(infinity) if x > 100 and x < infinity]
# replacing range with xrange in python2
但這只是一個步驟之前,我想轉換的方括號進入正常的,轉換我的名單變成發電機。如果你可以更精確地滿足要求,也許我們可以做得更好。
例如,您提到您運行各種功能。爲什麼不喜歡的東西替代它:
def clean_some_data(data, cleanup_functions):
return [cleanup_function(data) for cleanup_function in cleanup_functions]
# or
def clean_some_data(data, cleanup_functions):
for cleanup_function in cleanup_function:
data = [cleanup_function(item) for item in data]
return data
有一些可能性,但更多的細節將需要
知道...一般蟒蛇垃圾收集只是工作 –
您也可以使用'del'。 http://stackoverflow.com/questions/6146963/when-is-del-useful-in-python – NPE
爲什麼你需要執行你自己的運行時內存管理?在Python中,與大多數語言一樣,一旦釋放內存,運行時系統就會將其返回到可用的堆中。在Python中發佈它只是沒有更多的數據對象引用。 – Prune