user236215
user236215

Reputation: 7546

R loop for calculating matrix elements taking too long to execute

The following loop is taking a lot of time to run. If I comment the line inside, it runs quickly. prices is 3224x40 matrix.

get_returns <- function(prices, type=1) {  
  returns = prices
  returns2 = prices
  returns[,] = 0
  M = dim(prices)[1]
  N = dim(prices)[2]

  for (i in 2:M) {
    for (j in 1:N) {      
      returns[i,j] = prices[i,j]/prices[i-1,j] - 1   ## LINE 1
      returns2[i,j] = prices[i,j]/prices[1,j] - 1   ## LINE 1
    }
  }
}

> dim(prices)
[1] 3224   40

>
> system.time(get_returns(prices,1))
   user  system elapsed 
1213.95    1.12 1266.60 
> 

> # After commenting LINE 1
> system.time(get_returns(prices,1))
   user  system elapsed 
   0.67    0.00    0.75 
>

It is almost taking 20 minutes to run. I am wondering why would it take this long to run and if I can do something to optimize it. I have other loops which run fairly fast but this particular loop is a problem.

Upvotes: 0

Views: 1672

Answers (2)

Wojciech Sobala
Wojciech Sobala

Reputation: 7561

If prices is data.frame

sapply(prices,diff)/prices[-nrow(prices_df),]

If prices is matrix

apply(prices,2,diff)/prices[-NROW(prices),]

Upvotes: 0

Apprentice Queue
Apprentice Queue

Reputation: 2036

Loops take too long in R. You need to take advantage of vectorization. For example, instead of your loop, try this:

returns <- prices[2:M,]/prices[1:(M-1),] -1;

See also the information in the R inferno.

Upvotes: 4

Related Questions