Very slow loop

I just noticed that if you annotate the output of cat, the instability is solved in this case as well (at least in the MWE). The result is as good as using dims=Val(2), and probably more intuitive:

julia> function g(A,N)
         if N == 0
           println("got here")
           A = cat(A,A,dims=2)::Array{Float64,2}
         end
         s = 0.
         for a in A
           s += a
         end
         s
       end
g (generic function with 1 method)

julia>  A = rand(3,3);

julia> @btime g($A,1)
  5.923 ns (0 allocations: 0 bytes)
5.379437443399205


3 Likes