%timeit -n 10000 overlap(a,b)

10000 loops, best of 3: 28.3 µs per loop

%timeit -n 10000 overlap_mbk(a,b)

10000 loops, best of 3: 8.68 µs per loop

def overlap_mbk(a, b):

a1=np.argsort(a)

b1=np.argsort(b)

# use searchsorted:

sort_left_a=a[a1].searchsorted(b[b1], side=’left’)

sort_right_a=a[a1].searchsorted(b[b1], side=’right’)

#

sort_left_b=b[b1].searchsorted(a[a1], side=’left’)

sort_right_b=b[b1].searchsorted(a[a1], side=’right’)

# # which values are in b but not in a?

# inds_b=(sort_right_a-sort_left_a == 0).nonzero()[0]

# # which values are in b but not in a?

# inds_a=(sort_right_b-sort_left_b == 0).nonzero()[0]

# which values of b are also in a?

inds_b=(sort_right_a-sort_left_a > 0).nonzero()[0]

# which values of a are also in b?

inds_a=(sort_right_b-sort_left_b > 0).nonzero()[0]

return a1[inds_a], b1[inds_b]

]]>