實現一個系統,當涉及到繁重的數學提升時,我想盡可能少地執行。用於緩存的哈希Numpy對象的快速方法
我知道存在與numpy對象一起使用memoisation的問題,並且因此實現了一個lazy-key緩存以避免整個「過早優化」參數。
def magic(numpyarg,intarg):
key = str(numpyarg)+str(intarg)
try:
ret = self._cache[key]
return ret
except:
pass
... here be dragons ...
self._cache[key]=value
return value
但由於字符串轉換需要相當長的一段...
t=timeit.Timer("str(a)","import numpy;a=numpy.random.rand(10,10)")
t.timeit(number=100000)/100000 = 0.00132s/call
什麼人建議爲「更好的方式」來做到這一點?
[如何在Python中哈希大對象(數據集)?](http://stackoverflow.com/questions/806151/how-to-hash-a-large-object-dataset-in-python ) – tacaswell 2014-03-19 16:43:10