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 ___________________________________________________________ now. [[alternative HTML version deleted]]
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. > > UsmanIt 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/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
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.Why use a loop here at all? It seems like this would do it pretty fast: Re <- rnorm(n = 2e+06, mean = - 1)> system.time(Re <- rnorm(n = 2e+06, mean = - 1))[1] 0.77 0.01 0.78 NA NA I'm not sure what you expect Re to be (a list with 100000 vectors of length 20?), but you could reshape one long vector into a matrix or whatever you might need. For example: Re.mat <- matrix(Re, ncol=20)> Usman > > > > > ___________________________________________________________ > > now. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Chuck Cleland wrote: <snip>> Why use a loop here at all? It seems like this would do it pretty fast: > > Re <- rnorm(n = 2e+06, mean = - 1) > > > system.time(Re <- rnorm(n = 2e+06, mean = - 1)) > [1] 0.77 0.01 0.78 NA NA > > I'm not sure what you expect Re to be (a list with 100000 vectors of > length 20?), but you could reshape one long vector into a matrix or > whatever you might need. For example: > > Re.mat <- matrix(Re, ncol=20)<snip> Ummmmmm, not that it really matters, but doesn't the original construction (x-2*10)/20, give variates with mean = -1 and standard deviation = 1/20 = 0.05? (One wonders why ``(x-2*10)/20'' rather than ``(x-20)/20'', even if one allows for doing the operation in this convoluted way.) It might be added that ``Re'' is not a good name for an object since it is the name of a function (which returns the real part of complex numbers). cheers, Rolf Turner rolf at math.unb.ca
Yet another way of doing it:> system.time({+ x <- replicate(100000, (rnorm(20) - 20) / 20) + }) [1] 3.44 0.06 3.71 NA NA> > str(x)num [1:20, 1:100000] -1.041 -0.980 -0.987 -1.062 -1.012 ...>On 4/29/07, Usman Shehu <ugulumbe at yahoo.co.uk> 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 > > > > > ___________________________________________________________ > > now. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?