[R] While loop history

William Dunlap wdunlap at tibco.com
Mon Apr 8 01:52:19 CEST 2013


> I am aware of the apply() functions,
> but they are wrapper function of for loops, so they are slower.

While this sounds right in the abstract, it isn't always so.  Many times
looping code is slow not because of the function calls of interest but
because of how the memory used to store the result is managed and
other details in setting up the loop.  E.g., compare the following loops:
  > system.time({ x1 <- numeric(0) ; for(i in 1:50000) x1[i] <- log(i) })
     user  system elapsed 
     2.34    0.03    2.37 
  > system.time(x2 <- sapply(1:50000, log))
     user  system elapsed 
     0.08    0.00    0.08 
  > system.time({ x3 <- numeric(50000) ; for(i in 1:50000) x3[i] <- log(i) })
     user  system elapsed 
     0.11    0.00    0.11 
identical() shows that all those give the same results.  Using sapply() means
that you don't have to remember the rule about allocating the output vector
ahead of time, thus simplifying your code.

The built-in apply functions are also compiled, which saves some time.  You can
compile your own functions:
  > library(compiler)
  > f5 <- function(n,FUN) { x <- numeric(n) ; for(i in seq_len(n)) x[i] <- FUN(i) ; x }
  > f5_compiled <- cmpfun(f5)
  > system.time( x5 <- f5(50000, log) )
     user  system elapsed 
     0.09    0.00    0.09 
  > system.time( x5_compiled <- f5_compiled(50000, log) )
     user  system elapsed 
     0.02    0.00    0.01

The built-in vapply() allows you to specify the type (and size) of the return value
of FUN, which can save time also (that was also part of the speedup of f5() above).
  > system.time(x6 <- vapply(1:50000, log, FUN.VALUE=0.0))
     user  system elapsed 
     0.06    0.00    0.06
vapply() also checks that FUN(X[i]) returns a value of the expected type and size,
something the others did not do.

So you can write looping code that faster than the built-in *apply functions, but
it may be a fair bit more work than using them.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of C W
> Sent: Sunday, April 07, 2013 3:11 PM
> To: John Kane
> Cc: r-help at r-project.org; Baylee Smith
> Subject: Re: [R] While loop history
> 
> May I say also ask one thing?  @OP: sorry to use your post.
> 
> What would you use instead of loops?  I am aware of the apply() functions,
> but they are wrapper function of for loops, so they are slower.  At one
> point, I was told to go back to C for faster implementation, but I like R
> much more.
> 
> In the case of repeated simulation such as Monte Carlo, what would you use
> instead of for loop?
> 
> Mike
> On Sun, Apr 7, 2013 at 7:43 AM, John Kane <jrkrideau at inbox.com> wrote:
> 
> >
> > http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-
> example
> >
> > Loops are seldom a good solution in R so some more information and data
> > would be useful
> >
> > At the simplist, for your specific question  I think you could set up two
> > vectors (e.g. v1  <-  rep(NA, 10 ) and just write the values into the
> > vectors as you proceed through the loop.
> >
> > John Kane
> > Kingston ON Canada
> >
> >
> > > -----Original Message-----
> > > From: bayywater at gmail.com
> > > Sent: Sun, 7 Apr 2013 14:36:33 +1200
> > > To: r-help at r-project.org
> > > Subject: [R] While loop history
> > >
> > > Hi,
> > > I am new at R and still trying to get the hang of things.
> > > I am running a while loop and wish to save the results of each iteration.
> > > The results are a vector x of length two and I wish to save the results
> > > of
> > > each iteration as two vectors, one for x[1] and the other for x[2].
> > >
> > > Thanks!
> > >
> > >       [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> > > http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> >
> > ____________________________________________________________
> > GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at
> > http://www.inbox.com/smileys
> > Works with AIM(r), MSN(r) Messenger, Yahoo!(r) Messenger, ICQ(r), Google Talk(tm) and
> > most webmails
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> 	[[alternative HTML version deleted]]



More information about the R-help mailing list