Search code examples
rperformancevectorizationnested-loops

Increase speed with vectorisation


Is anyone able to help me improve the performance of this R function? I am trying to get it to run as fast as possible.

stochrickvect <- 
function(p0=runif(1000,.5,1.5),r=1.2,K=1,sigma=0.2,numyears=100)
{
   #initialize
   N<-matrix(NA,numyears,length(p0))
   N[1,]<-p0

   for (pop in 1:length(p0)) #loop through the populations
   {
     for (yr in 2:numyears) #for each pop, loop through the years
     {
       N[yr,pop]<-N[yr-1,pop]*exp(r*(1-N[yr-1,pop]/K)+rnorm(1,0,sigma))
     }
   }
   return(N)
 }

Thanks!


Solution

  • Vectorisation:

     stochrickvect <- 
    function(p0=runif(1000,.5,1.5), r=1.2, K=1, sigma=0.2, numyears=100)
     {
       N <- matrix(NA, numyears, length(p0)) #initialize
       N[1,] <- p0
    
       for (yr in 2:numyears) # loop through the years
       {
         N[yr,] <- N[yr-1,]*exp(r*(1-N[yr-1,]/K) + rnorm(length(p0),0,sigma))
       }
       return(N)
     }