I have the rather simple function func1()
defined below, mainly composed of two for
blocks.
It works perfectly for small N
values, but being that the for
blocks are combined it grows very quickly to the point of taking minutes when N>1000
.
How can I use numpy
broadcasting to improve the performance of this function?
import numpy as np
import time as t
def func1(A_triang, B_triang):
aa = []
for i, A_tr in enumerate(A_triang):
for j, B_tr in enumerate(B_triang):
# Absolute value of differences.
abs_diff = abs(np.array(A_tr) - np.array(B_tr))
# Store the sum of the differences and the indexes
aa.append([sum(abs_diff), i, j])
return aa
# Generate random data with the proper format
N = 500
A_triang = np.random.uniform(0., 20., (N, 3))
A_triang[:, 0] = np.ones(N)
B_triang = np.random.uniform(0., 20., (N, 3))
B_triang[:, 0] = np.ones(N)
# Call function.
s = t.clock()
aa = func1(A_triang, B_triang)
print(t.clock() - s)