Wolfgang Raffelsberger
2008-Sep-03 14:39 UTC
[R] optimizing speed of calculation (recursive product)
Dear list, I'm wondering how to optimize functions/processes like the one shown below (which simplifies something we're trying to do with very large datasets). In many cases I've noticed that using apply, sapply etc can help speeding up processes, but in this case I don't see how I could do so. a <- runif(10000000,0.5,1.6) C <- 2 M <- 10000000 system.time( for (i in 1:(M-1)) {C <- C* c(a[i],a[i+1])} ) By the way, I'm using R-2.7.2 on Win XP and/or Fedora Linux. Thank's in advance for any hints, Wolfgang Raffelsberger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Wolfgang Raffelsberger, PhD Laboratoire de BioInformatique et G?nomique Int?gratives CNRS UMR7104, IGBMC 1 rue Laurent Fries, 67404 Illkirch Strasbourg, France
Wolfgang Raffelsberger wrote:> Dear list, > I'm wondering how to optimize functions/processes like the one shown > below (which simplifies something we're trying to do with very large > datasets). > In many cases I've noticed that using apply, sapply etc can help > speeding up processes, but in this case I don't see how I could do so. > > a <- runif(10000000,0.5,1.6) > C <- 2 > M <- 10000000 > system.time( for (i in 1:(M-1)) {C <- C* c(a[i],a[i+1])} )Well, in principal you could say: a <- runif(10000000,0.5,1.6) C <- 2 M <- 10000000 system.time(C <- C * c(prod(a[-M]), prod(a[-1]))) but this is all completely inaccurate from a numerical point of view - and you probably end up with c(0, 0) which is the quickest way to calculate those numbers in similar accurate way. Uwe Ligges> > By the way, I'm using R-2.7.2 on Win XP and/or Fedora Linux. > > Thank's in advance for any hints, > Wolfgang Raffelsberger > > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . > Wolfgang Raffelsberger, PhD > Laboratoire de BioInformatique et G?nomique Int?gratives > CNRS UMR7104, IGBMC 1 rue Laurent Fries, 67404 Illkirch Strasbourg, > France > > ______________________________________________ > 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.
Petr PIKAL
2008-Sep-03 15:32 UTC
[R] Odp: optimizing speed of calculation (recursive product)
Hi r-help-bounces at r-project.org napsal dne 03.09.2008 16:39:07:> Dear list, > I'm wondering how to optimize functions/processes like the one shown > below (which simplifies something we're trying to do with very large > datasets). > In many cases I've noticed that using apply, sapply etc can help > speeding up processes, but in this case I don't see how I could do so. > > a <- runif(10000000,0.5,1.6) > C <- 2 > M <- 10000000 > system.time( for (i in 1:(M-1)) {C <- C* c(a[i],a[i+1])} )Maybe simple math? You want last two members of 2*cumprod(a). So> system.time(2*cumprod(a)[9999999:10000000])user system elapsed 1.97 0.04 2.00>shall be a little bit quicker then for cycle. But it is valid only for the above calculation. Regards Petr> > > By the way, I'm using R-2.7.2 on Win XP and/or Fedora Linux. > > Thank's in advance for any hints, > Wolfgang Raffelsberger > > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . > Wolfgang Raffelsberger, PhD > Laboratoire de BioInformatique et G?nomique Int?gratives > CNRS UMR7104, IGBMC > 1 rue Laurent Fries, 67404 Illkirch Strasbourg, France > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Seemingly Similar Threads
- alternatives to traditional least squares method in linear regression ?
- how to make a function recognize the name of an object/vector given as argument
- require() is not giving TRUE / FALSE statements ?
- Don't find a package !
- using Sweave, how to save a plot in a given size