[R] Integration in R

William Dunlap wdunlap at tibco.com
Tue Oct 2 20:34:08 CEST 2012


Another nitpick: don't use return() in the last statement.
It isn't needed, it looks like some other language, and
dropping it saves 8% of the time for the uncompiled code
(the compiler seems to get of it).

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 Berend Hasselman
> Sent: Tuesday, October 02, 2012 11:19 AM
> To: Rui Barradas
> Cc: r-help at r-project.org; Naser Jamil
> Subject: Re: [R] Integration in R
> 
> 
> On 02-10-2012, at 20:01, Rui Barradas <ruipbarradas at sapo.pt> wrote:
> 
> > Hello,
> >
> > Yes, it's possible to remove the loop. Since the loop is used to compute a running
> product and all we want is the final result, use the vectorized behavior of R and a final
> ?prod().
> > Seedup: another 2x. And 4x2 == 8 == 1 [decimal] order of magnitude.
> >
> >
> > lf2 <-function (x) {
> >   v<-1
> >   x1 <- x[1]
> >   x2 <- x[2]
> >   x3 <- x[3]
> >   x4 <- x[4]
> >   z1 <- exp(x1+x2*dose)
> >   z2 <- exp(x3+x4*dose)
> >   psi0<-1/((1+z1)*(1+z2))
> >   psi1<-z1*psi0
> >   v <- (psi0^y0)*(psi1^y1)*((1-psi0-psi1)^y2)
> >   return( prod(v) )
> > }
> >
> > lf2.c <- cmpfun(lf2)
> >
> > Hope this helps,
> 
> 
> Wonderful. It certainly does help.
> A single nitpick: the v <- 1 at the start of the function can now be removed.
> 
> I got a speedup of 7.5 compared to the very first version lf1.
> 
> Berend
> 
> ______________________________________________
> 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.




More information about the R-help mailing list