# Performance of variants of findall

**URL:** https://discourse.julialang.org/t/performance-of-variants-of-findall/31659
**Category:** Performance
**Created:** [November 29, 2019, 2:29pm UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659 "2019-11-29T14:29:18Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![j2b2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j2b2/32/3387_2.png) [@j2b2](https://discourse.julialang.org/u/j2b2)
#### Post date: [November 29, 2019, 2:29pm UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/1 "2019-11-29T14:29:18Z")

</div>

Here is a small session:

```julia
julia> using BenchmarkTools

julia> A = rand(100, 100)
100×100 Array{Float64,2} [...]

julia> @btime findall(A .> 0.5)
  39.951 μs (7 allocations: 85.06 KiB)
5077-element Array{CartesianIndex{2},1} [...]

julia> @btime findall(x -> x > 0.5, A)
  208.314 μs (17 allocations: 256.80 KiB)
5077-element Array{CartesianIndex{2},1} [...]

```

Why these timing differences between evaluations of two expressions that look so close and sensible ?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 30, 2019, 7:24am UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/2 "2019-11-30T07:24:41Z")

</div>

They are not the same (you can explore this with `@edit`, or the debugger). The first creates a temporary array for `A .> 0`, and then collects the `true` indexes. Both of these operations are very fast.

The second one goes through the generic iterator path. Perhaps it could optimized more, I am sure PRs doing this would be welcome.

---

<div class="post-metadata">

### Author: ![j2b2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j2b2/32/3387_2.png) [@j2b2](https://discourse.julialang.org/u/j2b2)
#### Post date: [November 30, 2019, 10:02am UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/3 "2019-11-30T10:02:58Z")

</div>

Thanks for your answer. Perhaps some warning in the documentation ([Arrays · The Julia Language](https://docs.julialang.org/en/v1/base/arrays/#Base.findall-Tuple%7BFunction,Any%7D)), about the case where the second argument is an array, would be useful.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [November 30, 2019, 10:04am UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/4 "2019-11-30T10:04:06Z")

</div>

Try to see if there is any effect of interpolating `A` into the expression with `$`

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 30, 2019, 12:45pm UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/5 "2019-11-30T12:45:31Z")

</div>

I don’t think there is an inherent reason for the performance difference here (which would of course warrant documentation), it is just waiting for someone to optimize it.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [November 30, 2019, 1:09pm UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/6 "2019-11-30T13:09:47Z")

</div>

See also the similar topic [Comprehension vs map and filter unexpected speeds](https://discourse.julialang.org/t/comprehension-vs-map-and-filter-unexpected-speeds/31314)

---

<div class="post-metadata">

### Author: ![le\_ackerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/le_ackerman/32/38731_2.png) [@le\_ackerman](https://discourse.julialang.org/u/le_ackerman)
#### Post date: [February 27, 2023, 12:13pm UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/7 "2023-02-27T12:13:18Z")

</div>

> [@Tamas\_Papp](#):
>
> Perhaps it could optimized more, I am sure PRs doing this would be welcome.

Sorry I am new,  
but both of these gives the same result say if i want to find the indexes where the maximum values occur, the first one is way faster…  
Can you please explain this more

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 27, 2023, 1:57pm UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/8 "2023-02-27T13:57:54Z")

</div>

```julia
julia> using BenchmarkTools

julia> A = rand(100, 100);

julia> @btime findall($A .> 0.5);
  9.208 μs (5 allocations: 84.36 KiB)

julia> @btime findall(x -> x > 0.5, $A);
  9.058 μs (5 allocations: 84.36 KiB)

```

seems same speed to me

---

<div class="post-metadata">

### Author: ![le\_ackerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/le_ackerman/32/38731_2.png) [@le\_ackerman](https://discourse.julialang.org/u/le_ackerman)
#### Post date: [February 28, 2023, 5:12am UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/9 "2023-02-28T05:12:55Z")

</div>

```julia

julia> @btime findall(x->x==maximum(b),b);
  49.071 ms (29504 allocations: 462.69 KiB)

julia> @btime findall(x->x==maximum(b),$b);
  48.989 ms (29504 allocations: 462.69 KiB)

julia> @btime findall(b.==maximum(b))
  9.910 μs (7 allocations: 5.72 KiB)

```

but when trying to find maximum there is a big difference.

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [February 28, 2023, 5:50am UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/10 "2023-02-28T05:50:53Z")

</div>

try this:

```julia
@btime let maxval=maximum($b); findall(x->x==maxval,$b); end;

```

---

<div class="post-metadata">

### Author: ![le\_ackerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/le_ackerman/32/38731_2.png) [@le\_ackerman](https://discourse.julialang.org/u/le_ackerman)
#### Post date: [February 28, 2023, 5:52am UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/11 "2023-02-28T05:52:49Z")

</div>

outside btime i can’t use $ it shows,

```julia
ERROR: syntax: "$" expression outside quote around REPL[187]:1
Stacktrace:
 [1] top-level scope
   @ REPL[187]:1

```

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [February 28, 2023, 5:53am UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/12 "2023-02-28T05:53:36Z")

</div>

Don’t use $ outside btime.

---

<div class="post-metadata">

### Author: ![le\_ackerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/le_ackerman/32/38731_2.png) [@le\_ackerman](https://discourse.julialang.org/u/le_ackerman)
#### Post date: [February 28, 2023, 5:55am UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/13 "2023-02-28T05:55:09Z")

</div>

Can you explain me more about this error…?  
And the usage of $

---

<div class="post-metadata">

### Author: ![le\_ackerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/le_ackerman/32/38731_2.png) [@le\_ackerman](https://discourse.julialang.org/u/le_ackerman)
#### Post date: [February 28, 2023, 5:56am UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/14 "2023-02-28T05:56:44Z")

</div>

what’s the point of $ if i can’t use it anywhere else except while benchmarking…

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [February 28, 2023, 5:58am UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/15 "2023-02-28T05:58:19Z")

</div>

> [@le\_ackerman](#):
>
> `@btime findall(x->x==maximum(b),$b);`

For every iteration, this calculates the maximum again and again. If there are 100x100 element, it will pass over the array 10,000 times. You only need to calculate the maximum _once_.

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [February 28, 2023, 6:04am UTC](https://discourse.julialang.org/t/performance-of-variants-of-findall/31659/16 "2023-02-28T06:04:02Z")

</div>

$ is used for [string interpolation](https://docs.julialang.org/en/v1/manual/strings/#string-interpolation) and code [expression interpolation](https://docs.julialang.org/en/v1/manual/metaprogramming/#man-expression-interpolation)—allowing you to splice a variable directly into a string or code expression. For example:

```julia
julia> x="world" # string
       "hello $x" # string interpolation
"hello world"

julia> y=:( a += 1 ) # expression
       :( if true; $y end ) # expression interpolation
:(if true
      #= REPL[2]:2 =#
      a += 1
  end)

```

Because `@btime` is a macro, it operates on an expression. The authors of `BenchmarkTools` decided that `$` would capture the variable from the environment and interpolate it into the expression, while passing through type information and blocking constant propagation.

All that to say: when you’re building strings and expressions, and sometimes when calling macros, `$` can be useful. But for normal code, `$` shouldn’t be dangling around.
