首先,如果我是你,我会通过封装您在功能提到的那些电话,并呼吁
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