I imagine this will be possible some day, but I can't predict when.
Currently, if your slices are contiguous (step size 1 in each range), there is
in fact another option: encode the slice with a CartesianRange. See
http://julialang.org/blog/2016/02/iteration/
Example:
julia> function foo(A, R)
s = zero(eltype(A))
for I in R
s += A[I]
end
s
end
foo (generic function with 1 method)
julia> function bar(A)
s = zero(eltype(A))
for j = 1:size(A,2)
s += foo(A, CartesianRange(CartesianIndex((1,j)),
CartesianIndex((size(A,1),j))))
end
s
end
bar (generic function with 1 method)
julia> A = rand(4,10^4);
julia> bar(A)
20026.547698847495
julia> @time 1
0.000008 seconds (148 allocations: 10.151 KB)
1
julia> @time bar
0.000001 seconds (3 allocations: 144 bytes)
bar (generic function with 1 method)
Look ma, no allocation!
--Tim
On Thursday, February 04, 2016 07:09:45 PM Nitin Arora wrote:
> Hello,
>
> I realize that the current best way to do array slices is to either use
> ArrayViews or SubArrays.
>
> But will it ever be possible (in future) to have slice of contiguous arrays
> without memory allocation ? How do other compiled languages like Fortran or
> C deal with this issue ?
>
> For example, given:
> M1 = rand(7,3)
> M = view(M1,1:6,1:3) # some memory allocation occurs here
> a= rand(3)
> c1 = zeros(6)
> c =view(c1) # some memory allocation occurs here
>
> would it be possible to do this step without array allocation in future ?
> c = M*a # 2 memory allocations
>
> I don't have enough of a background to know the limits of the array
> indexing/slicing algorithms. I am designing an optimization software in
> Julia and I am happy using ArrayViews for now but just wanted to know what
> can be expected in the future.
>
> thanks,
> Nitin