eachindex is much slower than normal indexing for matrices

390 views
Skip to first unread message

abc

unread,
Feb 25, 2016, 8:35:56 AM2/25/16
to julia-users
For the following matrix
my_matrix = randn(10000, 1000)
using eachindex to access all elements is much slower than using ranges or even for el in my_matrix, even though it says in the documentation (http://docs.julialang.org/en/release-0.4/stdlib/arrays/#Base.eachindex) that eachindex uses ranges for Arrays.

Some code and numbers:
julia> sum = 0.0
julia
> @time for iter in eachindex(my_matrix)
             sum
+= my_matrix[iter]
         
end
   
1.288944 seconds (50.00 M allocations: 915.519 MB, 3.36% gc time)
julia> sum = 0.0
julia
> @time for i in 1:10000, j in 1:1000
             sum
+= my_matrix[i,j]
         
end
   
0.681678 seconds (34.38 M allocations: 524.582 MB, 2.45% gc time)
julia> sum = 0.0
julia
> @time for el in my_matrix
             sum
+= el
         
end
   
1.063564 seconds (40.00 M allocations: 762.993 MB, 3.41% gc time)

Am I reading the documentation wrong, or is there something strange with the matrix indexing? Because as it is, I don't see any benefit of using anything different than simple ranges for manipulating matrices.

Stefan Karpinski

unread,
Feb 25, 2016, 8:50:04 AM2/25/16
to Julia Users
Can you try it not in global scope?

abc

unread,
Feb 25, 2016, 9:52:35 AM2/25/16
to julia-users
Ah, yes, this seems to have fixed the problem, thank you.

Now, when not in global scope, using eachindex is definitely the fastest approach.
I made some measurements over 1000 runs for each of the approaches in the original post, here are the averages:
Using eachindex: 0.0026683
Using ranges: 0.0041256
Using in: 0.0031200

I guess I'll stick with eachindex. :)

J Luis

unread,
Feb 25, 2016, 9:57:06 AM2/25/16
to julia-users
Incidentally, this errors in 0.5 (but works fine in 0.4)

julia> s=0.0;

julia
> m=rand(10000,1000);


julia
> @time for el in
m
              s
= s + el
             
end
ERROR
: UndefVarError: s not defined
 
[inlined code] from .\none:2
 
in anonymous at .\no file:4294967295
 
in eval at <invalid>:0

Yichao Yu

unread,
Feb 25, 2016, 9:57:35 AM2/25/16
to Julia Users
^^ I believe this one is also iterating in the wrong direction. Try
`for j in ..., for i in ...`

Yichao Yu

unread,
Feb 25, 2016, 9:58:44 AM2/25/16
to Julia Users
`for j in ... , i in ...` *

J Luis

unread,
Feb 25, 2016, 10:13:54 AM2/25/16
to julia-users
Nope

julia> s=0.0;

julia
> m=rand(10000,1000);


julia
> @time for i in 1:10000, j in 1:1000

       s
= s + m[i,j]

       
end
ERROR
: UndefVarError: s not defined
 
[inlined code] from .\none:2
 
in anonymous at .\no file:4294967295
 
in eval at <invalid>:0

Mauro

unread,
Feb 25, 2016, 11:28:32 AM2/25/16
to julia...@googlegroups.com
On Thu, 2016-02-25 at 15:57, J Luis <jmf...@gmail.com> wrote:
> Incidentally, this errors in 0.5 (but works fine in 0.4)
>
> julia> s=0.0;
>
> julia> m=rand(10000,1000);
>
> julia> @time for el in m
> s = s + el
> end
> ERROR: UndefVarError: s not defined
> [inlined code] from .\none:2
> in anonymous at .\no file:4294967295
> in eval at <invalid>:0

I think this is https://github.com/JuliaLang/julia/issues/10472, or
might be related. I posted a minimal test case there.
Reply all
Reply to author
Forward
0 new messages