On 02-10-2012, at 20:01, Rui Barradas <ruipbarra...@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@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.