0

Are there any resources available or is somebody able to explain the severe differences in speed depending on the way at which something is implemented in R?

I've been working with huge matrices and looking at execution times for functions in R.

ie, Why is applying an ifelse statement to a matrix so dramatically different in execution time in comparison to iterating along each element in the matrix?

#Execution time ~1.5s
#dat : list object containing 5 matrices
#command finds the lowest non zero value from dat, if 0 then inf
do.call(pmin, lapply(dat, function(x) ifelse(x == 0, Inf, x)))

Applying the above to a 1000x1000 matrix is sub 2 seconds, however applying in a couple of for loops that iterates along each element takes ~15.6 seconds.

I understand that it might be something to do with applying functions at the C level of R, I'd just like a clear explanation if possible.

Thanks

A_Skelton73
  • 1,150
  • 4
  • 12
  • 23
  • 1
    Search for "The R inferno" by Patrick Burns. – Roman Luštrik Jul 15 '13 at 22:11
  • Best answers to the question nominated as duplicate: http://stackoverflow.com/questions/7142767/why-are-loops-slow-in-r/7144801#7144801 and http://stackoverflow.com/questions/7142767/why-are-loops-slow-in-r/7142982#7142982 both of which start out "loops are not necessarily slow". – IRTFM Jul 15 '13 at 22:22
  • @A_Skelton73 Try to remove the `ifelse` call too, is not known to be slow. May be something like this `function(x) x[which(x == 0)] <- Inf; return(x)` will probably be faster – dickoa Jul 15 '13 at 22:40

0 Answers0