[R-SIG-Finance] xts() speed on data with date index

Jeff Ryan jeff.a.ryan at gmail.com
Sat Jul 25 16:00:31 CEST 2009


Gabor is correct.

In addition the operation you are looking at is the creation of an xts
object, which is usually happening once during typical use (or at
least far fewer times than other operations).

xts stores all times internally as POSIXct, for internal
efficiency/consistency purposes. The original time class is preserved
in an internal attribute that is used in calls where it makes sense to
convert for the user:

(1)index():
where you are extracting the 'original' index.  Transparently the
POSIXct is converted back to the time class you expect.  This carries
overhead, but again should be only occasionally used.  Direct access
to the index is via .index()

(2)print() or printing by default.
Conversion cost is irrelevant here, as it is assumed you will spend
far more time looking at the output than any cost to convert.

The logic is that everything is 'time', just with slightly different
internal representations.  The user shouldn't have to care about time
choice, just as the user should have to care about big-endian or
little-endian hardware storage.

There are some technical issues that are still not perfect of course.
Timezone handling can get tricky if you do a lot of strange subsetting
with *different* index classes.  This type of handling is entirely
impossible in most other time series classes, so the 'weakness' isn't
really valid, as it is still better than most, but it isn't perfect
... yet.

HTH
Jeff


On Sat, Jul 25, 2009 at 8:19 AM, Gabor
Grothendieck<ggrothendieck at gmail.com> wrote:
> The explanation for the time difference is likely that xts
> converts all time indexes to POSIXct internally whereas
> zoo works with them in their original form.   Thus the
> time difference would be expected to be the conversion time.
>
> Once its converted then xts would likely be faster for those
> xts operatoins where the xts code is in C.  In contrast zoo
> is 100% R although eventually it is anticipated that portions
> of zoo will be in C too.
>
> As xts in a subclass of zoo some of its operations are done
> in zoo so in that case there would not be much of a difference
> in speed or if xts handles the corresponding operation itself
> but in R then it would not be expected that there would be
> much difference either.
>
> On Sat, Jul 25, 2009 at 8:59 AM, michael li<michaellibeijing at gmail.com> wrote:
>> I like xts because  subsetting is easier.  Something like x["2009-08"].
>> But it seems that xts() is a little slower than zoo() when converting
>> data with date index instead of time index.
>>
>>> pr<-matrix(rnorm(20000),nrow=5000,ncol=4)
>>> t<-Sys.time()+(1:5000)*24*3600
>>> d<-as.Date(t)
>>> system.time(x<-zoo(pr,t))
>>   user  system elapsed
>>      0       0       0
>>> system.time(x<-zoo(pr,d))
>>   user  system elapsed
>>      0       0       0
>>> system.time(x<-xts(pr,t))
>>   user  system elapsed
>>      0       0       0
>>> system.time(x<-xts(pr,d))
>>   user  system elapsed
>>   0.34    0.00    0.35
>>
>> Regards,
>> Michael
>>
>> _______________________________________________
>> R-SIG-Finance at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only.
>> -- If you want to post, subscribe first.
>>
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>



-- 
Jeffrey Ryan
jeffrey.ryan at insightalgo.com

ia: insight algorithmics
www.insightalgo.com



More information about the R-SIG-Finance mailing list