2

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?

Berry Boessenkool
  • 1,506
  • 11
  • 15
  • 3
    I think this is largely addressed by the answers and comments [for this question](https://stackoverflow.com/questions/5533246/why-is-apply-method-slower-than-a-for-loop-in-r). As Hadley's answer says, "the point of the apply (and plyr) family of functions is not speed, but expressiveness." – neilfws Nov 25 '20 at 22:04

0 Answers0