Skip to content
Prev 31659 / 398506 Next

Loops and memory

On Tue, 06 May 2003 19:35:01 +0000, you wrote in message
<Law11-F110twoPhPVKP0002a2b8 at hotmail.com>:
I think the main issue nowadays is that your code will go much slower
if it is interpreted R code (as a for loop would be) than if it is
compiled C or Fortran code (the way "a + b" is implemented
internally).  In the old days, there was an additional penalty for
doing a loop (S tried to allocate memory each step through the loop,
but wouldn't clean up until the end), but that isn't normally a
problem in R.  

But as a general principle, I think you should always write your code
to be readable first; if it turns out to be too slow that way, then
worry about optimizing it.  As you gain experience in R you'll find
the vectorized versions of formulas more readable than the loops and
you'll need to redo things less often, but at first, loops may be the
best way to get the right answer fast.

Duncan Murdoch