Perform a multi-column, multi-dimensional search quickly

On my machine for the 1e6 example I find this to be an order of magnitude faster:

julia> function f4(x)
           res = Int64[]
           for i ∈ 1:size(x, 1)
               @inbounds if x[i, 1] == -5 && x[i, 2] == 3.0 && x[i, 3] == 2.0
                   push!(res, i)
               end
           end
           res
       end
f4 (generic function with 1 method)

julia> @btime f4($x);
  1.694 ms (6 allocations: 21.86 KiB)

Note that f3 and f4 above return only the integer indices of the matching rows (i.e. roughly a length 1,000,000/11^3 ≈ 751 vector for the example above with 11 randomly chosen integers from -5 to 5), while versions 1, and 2 give you a size(x, 1)-lenght vector of booleans.

To get such a vector with the speed of f4 you can do:

julia> function f5(x)
           res = fill(false, size(x, 1))
           for i ∈ 1:size(x, 1)
               @inbounds if x[i, 1] == -5 && x[i, 2] == 3.0 && x[i, 3] == 2.0
                   res[i] = true
               end
           end
           res
       end
f5 (generic function with 1 method)

julia> @btime f5($x);
  1.686 ms (2 allocations: 976.67 KiB)
2 Likes