[R] Efficiency question

Joris Meys
Wed, 09 Jun 2010 11:00:19 -0000

On Tue, Jun 8, 2010 at 11:53 PM, Worik R <worikr at gmail.com>
wrote:
> Given the following snippet....
> ?m.nf.xts <- xts(rep(0, length(index(m.xts))), order.by=index(m.xts))
>
> Does R know to cache the index(m.xts) or is it more efficient to say...
>
> m.i <- index(m.xts)
> ?m.nf.xts <- xts(rep(0, length(m.i)), order.by=index(m.i))
this is one call less to a function, and hence more efficient. I
suppose it has to be order.by=m.i

Btw, How should R know what to cache? If it would cache all
calculations, you'd be out of memory in no time.
If you have more questions like this, you can test it by yourself, eg :

> x <- sample(rnorm(100))

> id <- index(x) # is in this case equivalent to 1:100

> system.time(replicate(10^5,{rep(0,length(index(x)))[index(x)]}))
   user  system elapsed
   7.61    0.09    7.76

> system.time(replicate(10^5,{rep(0,length(id))[id]}))
   user  system elapsed
   1.49    0.06    1.58

Cheers
Joris


>
> ?
>
> cheers
> Worik
>
> ? ? ? ?[[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.
>



--
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php