- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.7k
Closed
Labels
performanceMust go fasterMust go faster
Description
Compared to manual iteration, the new cartesian iterator has a small performance penalty:
function sumcart_manual(A::AbstractMatrix)
    s = 0.0
    @inbounds for j = 1:size(A,2), i = 1:size(A,1)
        s += A[i,j]
    end
    s
end
function sumcart_iter(A)
    s = 0.0
    @inbounds for I in CartesianIndices(size(A))
        s += A[I]
    end
    s
end
A = rand(10^4,10^4);
sumcart_manual(A);
sumcart_iter(A);
julia> @time sumcart_manual(A)
elapsed time: 0.176363505 seconds (96 bytes allocated)
4.999591778337127e7
julia> @time sumcart_iter(A)
elapsed time: 0.250890195 seconds (96 bytes allocated)
4.999591778337127e7In this gist, I compare the code that gets generated. The comparison reveals that the CartesianIndex is not fully elided by the compiler.
Any ideas what's happening?
EDIT: the core problem is that next contains a branch, and testing done is a 2nd branch, so there are two branches per iteration. See #16035 (comment)
Metadata
Metadata
Assignees
Labels
performanceMust go fasterMust go faster