# Performance of findfirst in 0.7

**URL:** <https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160>\
**Category:** Performance\
**Created:** [April 4, 2018, 3:26pm UTC](https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160 "2018-04-04T15:26:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [April 4, 2018, 3:26pm UTC](https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160/1 "2018-04-04T15:26:02Z")

</div>

Hi All,

The performance of `findfirst(A, value)` has significantly been affected in 0.7. It’s understandable that the method is deprecated. But changing the functionality in a manner that performance is substantially affected can be a significant issue for applications where it has been used. Here are some numbers from 0.6 and 0.7 for comparison.

0.6:

```julia
julia> a = fill(1, 2048); @time findfirst(a, 5)
  0.000004 seconds (4 allocations: 160 bytes)
0

```

0.7:

```julia
julia> a = fill(1, 2048); @time findfirst(a, 4)
┌ Warning: `findfirst(A, v)` is deprecated, use `coalesce(findfirst(isequal(v), A), 0)` instead.
│ caller = top-level scope at util.jl:156
└ @ Core util.jl:156
  0.002726 seconds (372 allocations: 23.250 KiB)
0

```

The sheer number of allocations are good enough indication that there is lots more happening within the method now in 0.7. Because of this some code is almost crawling in their 0.7 implementation which were fairly fast in 0.6.

Please note that the measurements are after several runs so initial start up overheads have been eliminated already.

regards,

Sambit

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 4, 2018, 3:36pm UTC](https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160/2 "2018-04-04T15:36:37Z")

</div>

Did you miss the deprecation warning?

Using BenchmarkTools.jl:

0.6:

```julia
julia> @btime findfirst($a, 4)
  1.368 μs (0 allocations: 0 bytes)
0

```

0.7:

```julia
julia> @btime findfirst(isequal(4), $a) # returns `nothing`
  1.626 μs (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [April 4, 2018, 3:46pm UTC](https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160/3 "2018-04-04T15:46:42Z")

</div>

@kristoffer.carlsson Your suggested code cannot be run with 0.6 as `Fix2` was not defined in 0.6. Deprecation should mean the functionality will be removed not something that was working will not perform. Otherwise one has to keep on implementing 2 versions in the code till 0.7 is released.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 4, 2018, 3:51pm UTC](https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160/4 "2018-04-04T15:51:18Z")

</div>

The idea is to use Compat.jl and _only_ use the 0.7 version from now on. i.e. your code above should look like

```julia
using Compat

a = fill(1, 2048)
findfirst(isequal(4), a)

```

Alternatively, if you don’t want to use `Fix2` you can just do

```julia
findfirst(x -> x == 4, a)

```

---

<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:** [April 4, 2018, 3:51pm UTC](https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160/5 "2018-04-04T15:51:32Z")

</div>

> [@sambitdash](#):
>
> Otherwise one has to keep on implementing 2 versions in the code till 0.7 is released.

The kind people who write

> **[GitHub - JuliaLang/Compat.jl: Compatibility across Julia versions](https://github.com/JuliaLang/Compat.jl)**
>
> Compatibility across Julia versions. Contribute to JuliaLang/Compat.jl development by creating an account on GitHub.

already did this for you.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 4, 2018, 3:57pm UTC](https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160/6 "2018-04-04T15:57:47Z")

</div>

By the way, when did we get currying? I didn’t think any of this was going to happen until after 1.0? (When I thought we would get general currying with `_`.) It looks like there are some functions in `Base` that just have explicit currying methods, but I can’t see which. Is there a link to the issue?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [April 4, 2018, 4:01pm UTC](https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160/7 "2018-04-04T16:01:41Z")

</div>

[https://github.com/JuliaLang/julia/pull/26436](https://github.com/JuliaLang/julia/pull/26436)

---

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [April 4, 2018, 4:26pm UTC](https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160/8 "2018-04-04T16:26:08Z")

</div>

@ExpandingMan using compat fixes the performance issue. Thanks.

---

<div class="post-metadata">

**Author:** ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)\
**Post date:** [April 4, 2018, 8:33pm UTC](https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160/9 "2018-04-04T20:33:43Z")

</div>

> [@](#):
>
> findfirst(isequal(4), a)

Quite surprising, one looks at `isequal(4)` and has _no_ idea what this could be ☹

(but, ok - thanks for the link - read the discussion now in #26436 (don’t have an opinion))

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 4, 2018, 8:36pm UTC](https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160/10 "2018-04-04T20:36:34Z")

</div>

It reads pretty cleanly no? `findfirst(isequalto(4), a)` – find the first (element) that is equal to 4 in `a`.

---

<div class="post-metadata">

**Author:** ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)\
**Post date:** [April 4, 2018, 9:02pm UTC](https://discourse.julialang.org/t/performance-of-findfirst-in-0-7/10160/11 "2018-04-04T21:02:55Z")

</div>

Not sure. One has to think the ‘to’ part - the function is called ‘isequal’ and not ‘isequalto’ - but then yes, it reads pretty cleanly. If one just enters `isequal(4)` into the console to find out what this could mean, it won’t work. - Will try to read it as a sentence, this is a good hint.
