I'm preparing an R course and ran into an interesting problem:
# To prove loops take much longer than vectorized equivalents:
vec <- vec_orig <- rnorm(10e6)
system.time(cumsum(vec)) # 0.03 sec
system.time(for(i in 2:length(vec)) vec[i] <- vec[i-1] + vec[i]) # 0.95 sec
# If you can't avoid loops, at least get a progress bar:
vec <- vec_orig
vec_new <- pbapply::pbsapply(2:length(vec), function(i) vec[i-1] + vec[i]) # 13 secs
I already checked, base::sapply
itself is just as slow.
Any idea why sapply is slower than a for loop that also needs to overwrite the vector elements?