UPDATE: Note that the relevant function in Julia v1+ is view
Question: I would like to index into an array without triggering memory allocation, especially when passing the indexed elements into a function. From reading the Julia docs, I suspect the answer revolves around using the sub function, but can't quite see how...
Working Example: I build a large vector of Float64 (x) and then an index to every observation in x.
N = 10000000
x = randn(N)
inds = [1:N]
Now I time the mean function over x and x[inds] (I run mean(randn(2)) first to avoid any compiler irregularities in the timing):
@time mean(x)
@time mean(x[inds])
It's an identical calculation, but as expected the results of the timings are:
elapsed time: 0.007029772 seconds (96 bytes allocated)
elapsed time: 0.067880112 seconds (80000208 bytes allocated, 35.38% gc time)
So, is there a way around the memory allocation problem for arbitrary choices of inds (and arbitrary choice of array and function)?