Why findall() function is super slow?

Here is an example and I’m just wondering why it takes 51 mS to do this considering the input size is very small.

aa = [1,2,3,4,5,6]
bb = [2,5]
@time findall(x->x==bb, aa)

julia> @time findall(x->in(x, bb), aa)
  0.051113 seconds (78.99 k allocations: 4.014 MiB)
2-element Array{Int64,1}:
 2
 5

It is compiling the findall function. Run the timing twice in the same session and you will see it is much faster the second time. I would suggest using the BenchmarkTools package for microbenchmarks, as it takes care of that and a few other things.

1 Like

Separately, I’ve found in is pretty slow if the collection isn’t a set – you’ll get better performance converting bb to a set first.

No, that’s not it. See This post. OP, it’s because the anonymous function is compiling, and you compile it again every time you run that call in global scope.

Won’t be a problem if you have that command in a function.

8 Likes

Yes, I noticed that it’s a lot faster when it is used inside a function.

Nitpick: it’s “ms”. “mS” is millisiemens, a unit of conductance.

2 Likes