I have the following code which calculates the number of pairs 1 <= i < j <= n
such that xs[i] == ys[j]
:
def f(xs, ys):
s = 0
for j in range(xs.size):
s += np.sum(xs[:j] == ys[j])
return s
This is called a couple of times from some other procedure, so I need it to be as fast as possible (at some memory cost).
The sizes of the arrays are > 1e6
.
Is there a faster equivalent that makes use of some numpy magic which gets rid of the for
loop?