You can speedup the calculation by removing array()
calls, here is an example:
import numpy as np
B = np.random.rand(3, 32, 128)
a = np.random.rand(32, 32)
def f1(a, B):
Y = dot(a,B[0])
dY = dot(a,B[1])
ddY = dot(a,B[2])
return array([Y,dY,ddY])
def f2(a, B):
result = np.empty((B.shape[0], a.shape[0], B.shape[-1]))
for i in xrange(B.shape[0]):
np.dot(a, B[i], result[i])
return result
r1 = f1(a, B)
r2 = f2(a, B)
print np.allclose(r1, r2)
The result of f1()
and f2()
is the same, but the speed if different:
%timeit f1(a, B)
%timeit f2(a, B)
Result is:
1000 loops, best of 3: 1.34 ms per loop
10000 loops, best of 3: 135 µs per loop