Unfortunately I tested it only in REPL. Since I only tasted Julia the difficult part was to figure out how to generate the random, sorted arrays. The algorithm itself was a near match to the one I wrote in the first python/numba example - loop advancing the position of pointer to the smallest value from the two lists.
1 Like
Here-s the Julia code:
a = sort(unique(rand(1:2000, 100)))
b = sort(unique(rand(1:2000, 100)))
function overlap(a,b)
out = 0
i1 = 1
i2 = 1
s1 = size(a)[1]
s2 = size(b)[1]
while i1 <= s1 && i2 <= s2
v1 = a[i1]
v2 = b[i2]
if v1 == v2
out += 1
i2 += 1
i1 += 1
elseif v1 > v2
i2 += 1
else
i1 += 1
end
end
return out
end
overlap(a,b)
# uncomment the following two lines to install Benchmark Tools
# import Pkg
# Pkg.add("BenchmarkTools")
using BenchmarkTools
@benchmark overlap(a,b)
2 Likes