0

How to speed up the following code inside a foreach %dopar%?

it is already embedded inside a paralleled foreach loop. Therefore, it better uses only one core. But Is there any way to reduce its computational time a little bit with single core? Thank you.

n = 1e5
k = 10

mu = 0
sigma = 1
x = matrix(data=NA, nrow=n, ncol=k)

for(j in 1:k) {
    for(i in 1:n) {
        x[i,j] = rnorm(1, mu, sigma)
    }
}
Mike Zhang
  • 21
  • 1
  • 4
    `x = matrix( rnorm(n*k,mean=mu,sd=sigma), n, k)` would be the vectorized approach, should be much faster, about 35x faster at this scale and bigger difference as you expand loops. Answered here: https://stackoverflow.com/questions/11640415/generate-matrix-with-iid-normal-random-variables-using-r. Why to use vectorization: https://www.noamross.net/archives/2014-04-16-vectorization-in-r-why/ – Jon Spring Feb 20 '22 at 23:23

1 Answers1

1

Try this:

f1 = function(){
n = 1000
k = 1000
  
mu = 0
sigma = 1
x = matrix(data=NA, nrow=n, ncol=k)
  
for(j in 1:k) {
   for(i in 1:n) {
    x[i,j] = rnorm(1, mu, sigma)
  }
 }
}


f2 = function(){
  n = 1000
  k = 1000
  
  mu = 0
  sigma = 1
  
  data = rnorm(n*k, mu, sigma)
  x = matrix(data=data, nrow=n, ncol=k)
}
system.time(f1())
system.time(f2())

f2() is almost 32 times faster

Ekapunk
  • 53
  • 5
  • Upvoted. Good answer. I figured out the vector to matrix trick a while ago myself. Whenever anybody has an urge to use a for loop in R, they should mediate on vectorization for few moments until it goes away. – SteveM Feb 20 '22 at 23:39