Bol*_*ter 20 python performance numpy
实施一个系统,当涉及到繁重的数学提升时,我想尽可能少地做.
我知道存在与numpy对象进行memoisation的问题,因此实现了一个惰性密钥缓存以避免整个"过早优化"参数.
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
Run Code Online (Sandbox Code Playgroud)
但由于字符串转换需要很长时间......
t=timeit.Timer("str(a)","import numpy;a=numpy.random.rand(10,10)")
t.timeit(number=100000)/100000 = 0.00132s/call
Run Code Online (Sandbox Code Playgroud)
人们认为做"更好的方式"是什么意思?
sen*_*rle 24
借用这个答案 ......所以我猜这是重复的:
>>> import hashlib
>>> import numpy
>>> a = numpy.random.rand(10, 100)
>>> b = a.view(numpy.uint8)
>>> hashlib.sha1(b).hexdigest()
'15c61fba5c969e5ed12cee619551881be908f11b'
>>> t=timeit.Timer("hashlib.sha1(a.view(numpy.uint8)).hexdigest()",
"import hashlib;import numpy;a=numpy.random.rand(10,10)")
>>> t.timeit(number=10000)/10000
2.5790500640869139e-05
Run Code Online (Sandbox Code Playgroud)
from joblib import Memory
location = './cachedir'
memory = Memory(location)
# Create caching version of magic
magic_cached = memory.cache(magic)
result = magic_cached(...)
# Or (for one-time use)
result = memory.eval(magic, ...)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9722 次 |
最近记录: |