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