Are R loops really that slow?
“For-loops are not idiomatic R code” is something I have heard many times. In trivial cases or fast prototyping, for-loops are fantastic yet I have always converted them into base::apply
functions, or pbapply::pbapply
for progress bars or even parallel::parApply
for parallel computation afterwards.
However, I honestly hadn’t looked into why R loops are slow before. The top result on Google suggests that “Loops are slower in R than in C++ because R is an interpreted language (not compiled)” but I wanted to take an in-depth look. I hoped understand if apply does anything special under the hood or if it is syntactic sugar similar to my analogy of $
signs in Excel formulas.
Vectorisation
My first thought was that apply must be doing something special, such as vectorisation. To my surprise, apply
is just a fancy wrapper for a loop with smart pre-allocation! So my next challenge was to try write a loop as fast as apply using pre-allocated memory for results.