Skip to content
Prev 170697 / 398506 Next

Alternate to for-loop

A couple of remarks on vQ's naive benchmark:
I suppose you meant

	f.rep = function(n, m) replicate(n, mean(rnorm(m)))

which doesn't make a substantial speed difference, though.
True, I get the same timing results on my machine.  But then you  
should also point out that the original for-loop:

	f.for = function(n, m) { res <- numeric(n); for (i in 1:n) res[i] <-  
mean(rnorm(m)); res }

is exactly as fast as replicate().  So apart from "looking more  
professional", there isn't any difference between an explicit loop and  
replicate().

Perhaps loops in R aren't always as slow (compared to matrix  
operations) as one seemed to think.  I ran into a similar issue with a  
simple benchmark the other day, where a plain loop in Lua was faster  
than vectorised code in R ...


I have to say, though, that like Patrick I assumed the goal was to  
obtain a large number of replicates for relatively small sets of  
random numbers, in which case the matrix solution is indeed faster  
(though not as much as I would have thought):

 > system.time(f.for(100000, 100))
    user  system elapsed
   4.212   0.025   4.273
 > system.time(f.rep(100000, 100))
    user  system elapsed
   4.109   0.028   4.172
 > system.time(f.pat(100000, 100))
    user  system elapsed
   1.580   0.134   1.739


Best regards,
Stefan Evert

[ stefan.evert at uos.de | http://purl.org/stefan.evert ]


PS: Don't feed trolls who say that Lua is better than R. ;-)