Re: [R] Efficiency question

From: Joris Meys <jorismeys_at_gmail.com>
Date: Wed, 09 Jun 2010 13:00:19 +0200

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

______________________________________________
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.
Received on Wed 09 Jun 2010 - 11:03:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Jun 2010 - 11:40:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive