I know that loops are slow in R
and that I should try to do things in a vectorised manner instead.
But, why? Why are loops slow and apply
is fast? apply
calls several sub-functions -- that doesn't seem fast.
Update: I'm sorry, the question was ill-posed. I was confusing vectorisation with apply
. My question should have been,
"Why is vectorisation faster?"
Loops are slower in R than in C++ because R is an interpreted language (not compiled), even if now there is just-in-time (JIT) compilation in R (>= 3.4) that makes R loops faster (yet, still not as fast). Then, R loops are not that bad if you don't use too many iterations (let's say not more than 100,000 iterations).
Originally Answered: Why are for loops so slow in Matlab? The loops incur much overhead of MATLAB, the interpreted language, calling many compiled code routines. It calls the compiled code routines each time it re-enters the body of the loop.
Beyond performance limitations due to design and implementation, it has to be said that a lot of R code is slow simply because it's poorly written. Few R users have any formal training in programming or software development. Fewer still write R code for a living.
It's not always the case that loops are slow and apply
is fast. There's a nice discussion of this in the May, 2008, issue of R News:
Uwe Ligges and John Fox. R Help Desk: How can I avoid this loop or make it faster? R News, 8(1):46-50, May 2008.
In the section "Loops!" (starting on pg 48), they say:
Many comments about R state that using loops is a particularly bad idea. This is not necessarily true. In certain cases, it is difficult to write vectorized code, or vectorized code may consume a huge amount of memory.
They further suggest:
- Initialize new objects to full length before the loop, rather than increasing their size within the loop.
- Do not do things in a loop that can be done outside the loop.
- Do not avoid loops simply for the sake of avoiding loops.
They have a simple example where a for
loop takes 1.3 sec but apply
runs out of memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With