For loops in R, they are obviously slow, but there is one more problem. It is much faster to pre-distribute the result vector, res, and add to res at each iteration.
Below we can compare the speed of the above version with the version that simply starts with the res vector, length N and changes the ith element during the cycle.
fn1 <- function(N) { res <- c() for (i in 1:N) { x <- rnorm(2) res <- c(res,x[2]-x[1]) } res } fn2 <- function(N) { res <- rep(0,N) for (i in 1:N) { x <- rnorm(2) res[i] <- x[2]-x[1] } res } > N <- 50000 > system.time(res1 <- fn1(N)) user system elapsed 6.568 0.256 6.826 > system.time(res2 <- fn2(N)) user system elapsed 0.452 0.004 0.496
Also, as Sharpie points out , we can do this a little faster using R functions like apply (or its siblings, sapply and lapply ).
fn3 <- function(N) { sapply( 1:N, function( i ){ x <- rnorm(2); return( x[2] - x[1] ) } ) } > system.time(res3 <- fn3(N)) user system elapsed 0.397 0.004 0.397
Christopher DuBois Jul 23 '09 at 4:19 2009-07-23 04:19
source share