# Why findall() function is super slow?

**URL:** <https://discourse.julialang.org/t/why-findall-function-is-super-slow/56222>\
**Category:** Performance\
**Created:** [March 1, 2021, 1:56am UTC](https://discourse.julialang.org/t/why-findall-function-is-super-slow/56222 "2021-03-01T01:56:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [March 1, 2021, 1:56am UTC](https://discourse.julialang.org/t/why-findall-function-is-super-slow/56222/1 "2021-03-01T01:56:20Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [March 1, 2021, 2:01am UTC](https://discourse.julialang.org/t/why-findall-function-is-super-slow/56222/2 "2021-03-01T02:01:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)\
**Post date:** [March 1, 2021, 2:04am UTC](https://discourse.julialang.org/t/why-findall-function-is-super-slow/56222/3 "2021-03-01T02:04:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 1, 2021, 2:05am UTC](https://discourse.julialang.org/t/why-findall-function-is-super-slow/56222/4 "2021-03-01T02:05:31Z")

</div>

No, that’s not it. See [This post](https://discourse.julialang.org/t/does-julia-have-equivalent-of-python-df-column-map-dict/47048/9). 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.

---

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [March 1, 2021, 2:12am UTC](https://discourse.julialang.org/t/why-findall-function-is-super-slow/56222/5 "2021-03-01T02:12:15Z")

</div>

> [@pdeffebach](#):
>
> This post

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

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 1, 2021, 3:55am UTC](https://discourse.julialang.org/t/why-findall-function-is-super-slow/56222/6 "2021-03-01T03:55:11Z")

</div>

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