I have two lists list1 and list2, each of size 5000 with every entry of the lists being a numpy.array. I want to calculate the squares of the Euclidean distances between the elements of the lists in a fast and efficient way, i.e. I need to calculate sum((list1[i]-list2[j])**2) for every combination of i and j, which are thus in total 2,500,000 combinations. I currently did so by running a double loop and writing every result into a 2d numpy.array by means of
result[i,j] = sum((list1[i]-list2[j])**2)
but still need on my computer about 4 minutes of time. I was wondering, whether any tricks could be used to further speed the calculation up.
scipy.spatial.distance.cdist.from scipy.spatial import distance_matrix; dist_mat = distance_matrix(list1, list2)?