title: Speed up resampling from array
body: <p>I'm doing a lot of resampling from large arrays, by row, and I was wondering if there's a way to speed it up. For example</p>
  
  <pre><code>n = 10^4
  a = rand(Float64, (n,n))
  @time r = a[sample(1:n,n),:]
  </code></pre>
  
  <p>I usually get about 0.8 seconds on my machine. <code>sample()</code> itself is quite fast. Indeed, <code>r = a[1:n, :]</code> is just about as slow as the above. I know these are large arrays, but am I missing something obvious? An order of magnitude speedup would be wonderful...</p>
  
  <p>EDIT: I have selected Przemyslaw Szufel's answer as it is nice and comprehensive, and definitely much faster if you're not going to further manipulate the array. Unfortunately in my case, Fredrik Bagge's caution proved true: it was not overall faster to use views, because subsequent operations to the array become slower--it was basically a wash in my testing. Also, Oscar Smith made a good point about column major. In my case, I later do something across the other dimension that's even more expensive the resampling so it made sense to leave the array as it is.   </p>