Matlab S ( R) FORTRAN .
S/R for-loops "" , , . , R Fortran C, . , sample, , for-loop, .
MATLAB ? : .
MathWorks, MATLAB, - , 2000 . . - Just-In-Time (JIT), script . . !
R , R R . R core R, R- . , . !
... , R MATLAB, , :
system.time(GRPdur(10^6)) # 9.50 secs
# Compile the function...
f <- compiler::cmpfun(GRPdur)
system.time(f(10^6)) # 3.69 secs
, for-loop 3 , . , R JIT , MATLAB.
UPDATE - R ( ), , @joran :
f <- function(n) {
p <- integer(n)
p[1] <- 1L
rv <- runif(n, 1, 1:n)
for (k in 2:n) {
r <- rv[k]
p[k] = p[r]
p[r] = k
}
p
}
g <- compiler::cmpfun(f)
system.time(f(1e6))
system.time(g(1e6))
system.time(GRPdur1(10^6))
system.time(GRPdur2(10^6))
... , MATLAB. sample sample.int, , -, MATLAB randperm 3x!
system.time(sample.int(10^6)) # 0.03