Issue
Implementing a system where, when it comes to the heavy mathematical lifting, I want to do as little as possible.
I'm aware that there are issues with memoisation with numpy objects, and as such implemented a lazy-key cache to avoid the whole "Premature optimisation" argument.
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
but since string conversion takes quite a while...
t=timeit.Timer("str(a)","import numpy;a=numpy.random.rand(10,10)")
t.timeit(number=100000)/100000 = 0.00132s/call
What do people suggest as being 'the better way' to do it?
Solution
Borrowed from this answer... so really I guess this is a duplicate:
>>> 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
Answered By - senderle
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.