假设我有一种从给定参数创建字典的方法:
def newDict(a,b,c,d): # in reality this method is a bit more complex, I've just shortened for the sake of simplicity
return { "x": a,
"y": b,
"z": c,
"t": d }
我还有另一种方法,每次执行时都会调用 newDict 方法。因此,最后,当我查看我的 cProfiler 时,我会看到如下内容:
17874 calls (17868 primitive) 0.076 CPU seconds
当然,我的 newDict 方法称为1785
times。现在,我的问题是我是否可以记住 newDict 方法以减少调用时间?(只是为了确保变量几乎在每次调用中都会发生变化,尽管我不确定它是否对记忆函数有影响)
子问题:我认为17k调用太多了,代码效率不高。但是通过查看统计数据,您是否也可以说明这是正常结果还是我有太多调用并且代码很慢?