[R] Loops and memory

R A F raf1729 at hotmail.com
Tue May 6 21:35:01 CEST 2003


The other day I was surprised by how much longer a for loop takes to
add two vectors a and b compared to a + b.  (I think that I made a and
b have a million entries.)

I guess my problem is that I don't really what the issues are, I guess,
so it's not clear to me when and where loops should be avoided.  I
guess I should try to get a copy of this new book to find out.

Thanks again.

>From: Prof Brian Ripley <ripley at stats.ox.ac.uk>
>To: R A F <raf1729 at hotmail.com>
>CC: r-help at stat.math.ethz.ch
>Subject: Re: [R] Loops and memory
>Date: Tue, 6 May 2003 20:30:22 +0100 (BST)
>On Tue, 6 May 2003, R A F wrote:
> > I'm afraid that I don't have your new book with Venables handy.  So
> > would it be fair to assume that there's no real need to avoid loops
> > these days?
>No, but the issues are different from those in 1996.  It is a lot less
>common to have to avoid loops, simply because memory can often be
>squandered.  But vectorizing calculations still pays off, sometimes
>handsomely: there is an example in that book of going from several hours
>to one second (and it's a real example).

More information about the R-help mailing list