Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add faster implementation for findall(::AbstractVector{Bool}) #42265

Closed
wants to merge 2 commits into from

Conversation

mzgubic
Copy link

@mzgubic mzgubic commented Sep 15, 2021

julia> a = rand(Bool, 10000);

julia> @btime findall($a);
  40.073 μs (2 allocations: 38.95 KiB)

julia> function myfindall(a)
           return (1:length(a))[a]
       end

julia> @btime myfindall($a);
  33.978 μs (2 allocations: 38.95 KiB)

though seems to be somewhat slower with BitArrays

julia> b = BitArray(rand(Bool, 10_000));

julia> @btime findall($b);
  6.113 μs (2 allocations: 39.89 KiB)

julia> @btime myfindall($b);
  6.224 μs (2 allocations: 39.89 KiB)

Thanks to @eperim for finding this

base/array.jl Outdated Show resolved Hide resolved
Co-authored-by: Lyndon White <[email protected]>
@jakobnissen
Copy link
Contributor

There is already: #42202

@mzgubic
Copy link
Author

mzgubic commented Sep 16, 2021

Oh, nice! Is your approach even faster for the benchmarks above?

@jakobnissen
Copy link
Contributor

jakobnissen commented Sep 16, 2021

Yes - on my branch:

julia> a = rand(Bool, 10000);

julia> using BenchmarkTools

julia> @btime findall($a);
  8.097 μs (2 allocations: 38.92 KiB)

julia> function myfindall(a)
                  return (1:length(a))[a]
              end
myfindall (generic function with 1 method)

julia> @btime myfindall($a);
  29.697 μs (2 allocations: 38.92 KiB)

@mzgubic
Copy link
Author

mzgubic commented Sep 16, 2021

Excellent!

@mzgubic mzgubic closed this Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants