On Sun, 2007-04-29 at 06:56 -0700, Usman Shehu wrote: > Greetings, > I have the following simple function but what worries me is that it > takes about 5 or more minutes to execute. My machine runs on windows > with 1.8GHz and 256 Ram. > > Re=NULL > > for(i in 1:100000){ > + x=rnorm(20) > + Re[i]=(x-2*10)/20 > + Re > + } > I would appreciate any help on how to make it faster. > > Usman
It is not clear exactly what you want to do, but taking what you wrote literally, there are 3 problems that I see: 1. You haven't allocated sufficient storage space for 'Re'. As such, at each loop, R has to copy and enlarge the object which take a all the time. 2. The result of (x-2*10)/20 is a vector of length 20, which you are trying to force into the space for a vector of length 1 3. In a loop like this, the last line containing just 'Re' does nothing. If you want 'Re' printed to the console, then you need to wrap it in print. Quite why you'd want 'Re' flashing up on the screen 100 000 times is beyond me... Fixing each of these gives: ## number of permutations n.perm <- 100000 ## storage space for a 100 000 x 20 matrix Re <- matrix(ncol = 20, nrow = n.perm) ## set up loop for(i in seq_len(n.perm)) { x <- rnorm(20) ## store in a row of Re Re[i,] <- (x-2*10)/20 } Timing this shows that it runs in 3.5 seconds on my desktop - which has similar processor but a lot more RAM: > system.time({ + n.perm <- 100000 + Re <- matrix(ncol = 20, nrow = n.perm) + for(i in seq_len(n.perm)) { + x <- rnorm(20) + Re[i,] <- (x-2*10)/20 + } + }) user system elapsed 3.336 0.056 3.394 HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC [f] +44 (0)20 7679 0565 UCL Department of Geography Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street London, UK [w] http://www.ucl.ac.uk/~ucfagls/ WC1E 6BT [w] http://www.freshwaters.org.uk/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.