Skip to content

Performance of logical indexing in Julia 0.7 #29418

Closed
@yanncalec

Description

@yanncalec

According to the documentation

Indexing by a boolean vector B is effectively the same as indexing by the vector of integers that is returned by findall(B).

This seems to be confirmed on 1d array. However a big difference of performance can be observed on 2d array. Compare the following two ways of logical indexing

X = rand(10^4,10^4)
idx = X.>0.5

# method 1:  indexing by boolean vector
@time for n=1:size(X,2)
    view(X, idx[:,n], n) 
end

# method 2:  indexing by position
@time for n=1:size(X,2)
    view(X, findall(idx[:,n]), n) 
end

In general the second method is 2x more efficient

 0.857407 seconds (138.47 k allocations: 398.172 MiB, 37.71% gc time)
 0.413342 seconds (118.47 k allocations: 397.715 MiB, 13.95% gc time)

and the gap can be even larger in realistic situations.

Metadata

Metadata

Assignees

Labels

arrays[a, r, r, a, y, s]performanceMust go fasterregressionRegression in behavior compared to a previous version

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions