I'm curious how much faster you expect it to be? Your example shows is that permuting 100 indices, an array access, and a sum take about 70 times longer than an array access and a sum by themselves. Seems reasonable to me...
Kevin On Thursday, October 16, 2014, K Leo <[email protected]> wrote: > I need to permute the indexes of an array, so I found function randperm > very convenient. But it turns out very slow. The following test shows > time increased 60 times. Is there an alternative to permute an array? > > ------------- > tic() > f=0. > A=[1:100] > for i=1:10000000 > f += A[20] > end > toc() > > elapsed time: 0.891431009 seconds > > ------------- > tic() > f=0. > A=[1:100] > for i=1:10000000 > A = randperm(100) > f += A[20] > end > toc() > > elapsed time: 60.348117087 seconds > >
