[R] loops in R - about inefficiency

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jun 24 11:53:06 CEST 2005


On Fri, 24 Jun 2005, Vehbi Sinan Tunalioglu wrote:

> Can someone technically explain, why does it take so long with loops in R?

First please demonstrate that it does!

a <- 0
system.time(for(i in 1:1e6) a <- a+1)

takes 1 second for a million iterations and you think that is `so long'?
If you do, the technical explanation is that R is interpreted rather than 
compiled.

More likely the problem is *how* you are using loops.

The FAQ mentions reference material on how to use R efficiently and it 
would be more productive to learn to do so rather than claim it is 
inefficient.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list