The reason is the usual global variable problem. Because n is global the
type is not inferred well and the loop is therefore not well optimized.
This doesn't happen in local scope e.g.
julia> let
m = 1000000
@allocated for i = 1:m end
end
0
and it can also be avoided if you specify the type in the loop range
julia> @allocated for i = 1:n::Int
end
0
2015-01-08 22:02 GMT-05:00 Petr Krysl <[email protected]>:
> julia> a=rand(1000000);
>
> julia> n=length(a);
>
> julia> @allocated for j=1:n
> end
> 63983688
>
> 64 MB to run this loop? Is that expected?
>
> Thanks for any insight.
>
> Petr
>