julia> @time asd=randperm(300000000)
43.437829 seconds (6 allocations: 2.235 GB, 0.01% gc time)
300000000-element Array{Int64,1}:
In R
> start = Sys.time()
> asd = sample(300000000)
> Sys.time()-start
Time difference of 23.27244 secs
Another way to go about it, is to look at R's implementation of a random permutation and recreate it in Julia, hoping for similar performance. Having done so, the resulting Julia code is:function nrandperm(r::AbstractRNG, n::Integer)
res = Array(typeof(n),n)
if n == 0
return res
end
a = typeof(n)[i for i=1:n]
nn = n
@inbounds for i = 1:n
j = floor(Int,nn*rand(r))+1
res[i] = a[j]
a[j] = a[nn]
nn -= 1
end
return res
end
nrandperm(n::Integer) = nrandperm(Base.Random.GLOBAL_RNG, n)
A size 1,000,000 permutation was generated x2 faster with this method.But, this method uses uniform floating random numbers, which might not be uniform on integers for large enough numbers. In general, it should be possible to optimize a more correct implementation. But if R envy is a driver, R performance is recovered with a pure Julia implementation (the R implementation is in C).
Unfortunately, this makes the implementation GPL. If you can describe the algorithm in an issue on github, someone can do a cleanroom implementation.
-viral
On Saturday, January 23, 2016 at 3:17:19 PM UTC+5:30, Dan wrote:
Another way to go about it, is to look at R's implementation of a random permutation and recreate it in Julia, hoping for similar performance. Having done so, the resulting Julia code is:
<GPL code removed>
A size 1,000,000 permutation was generated x2 faster with this method.
As requested I deleted the post. But algorithms for creating permutations are standard and very much in the public domain (what does TheArtOfProgramming say?). If someone really invests a little effort into it, a more formally correct algorithm can be implemented.
On Saturday, January 23, 2016 at 4:43:36 PM UTC+2, Kevin Squire wrote: