This block of code is already somewhat optimized thanks to some answers given over at Stack Overflow. In the last question I made (Improve performance of function without parallelization) the code was marginally improved but I later had to re-write a little bit of it and the changes proposed didn't apply anymore.
I'm looking at optimizing this block of code without resorting to parallelization, other than that I'm pretty much open to using any package out there.
I think this is perhaps the correct site to post this since it's really more of a question about how can I improve my code rather than something not working with it.
Here's the MWE (minimum working example):
import numpy as np
import timeit
def random_data(N):
# Generate some random data.
return np.random.uniform(0., 10., N)
# Data lists.
array1 = np.array([random_data(4) for _ in range(1000)])
array2 = np.array([random_data(4) for _ in range(2000)])
def func():
lst = []
for elem in array1:
# Define factors.
a_01, a_11 = max(elem[1], 1e-10), max(elem[3], 1e-10)
a_02, a_12 = array2[:,1], array2[:,3]
# Combine the factors defined above.
a_0 = a_01**2 + a_02**2
a_1 = a_11**2 + a_12**2
Bs, Cs = -0.5/a_0, -0.5/a_1
# Perform operations.
A = 1./(np.sqrt(a_0*a_1))
B = Bs*(elem[0]-array2[:,0])**2
C = Cs*(elem[2]-array2[:,2])**2
ABC = A*np.exp(B+C)
lst.append(max(ABC.sum(), 1e-10))
return lst
# Time function.
func_time = timeit.timeit(func, number=10000)
print func_time
array1
"belongs" (or "is related") toarray2
. \$\endgroup\$