Question

Could you guess which of the following option would run faster in R?

Source: stackoverflow

Option A
option_A = function(x=0){
  for(i in 1:100){
    x=x+i
    return(x)
  }
}
Option B
option_B = function(y=0){
  for(i in 1:100){
    ((((((((((y=y+i))))))))))
    return(y)
  }
}

Answer

Option A would run faster. As R treats ( as an operator and at each occurence of (, it has to do a name lookup. As you can see from the following benchmark test, option B is slower than option A. Moreover the code in the option can further be optimized. As you can see in, with each iteration of foor loop we are returning x, which should be avoided if x has to be returned only once.

library(microbenchmark)
print(
  summary(
    microbenchmark(
      option_A(x=0),
      option_B(y=0),
      times = 100,
      unit = 'ns' #nanosecond
    )
  )
)
##              expr min  lq   mean median   uq      max neval
## 1 option_A(x = 0) 600 800  73150   1100 1300  6365500   100
## 2 option_B(y = 0) 600 800 231210   1000 1200 20936500   100