[R-SIG-Finance] xts() speed on data with date index
Gabor Grothendieck
ggrothendieck at gmail.com
Sun Jul 26 04:09:54 CEST 2009
The conversion to or from Date occurs when you store it (as you
noticed) or print it out or otherwise try to use it in a context that
requires a Date. Your example does not do any of those so no
conversion was needed. Try this:
> system.time(print(x))
...
user system elapsed
0.39 0.00 0.39
> system.time(print(t))
...
user system elapsed
0.08 0.00 0.11
On Sat, Jul 25, 2009 at 9:38 PM, Michael<michaellibeijing at gmail.com> wrote:
> Thanks for detailed explanation and demo codes.indexClass(x)<-"Date is
> great for its speed. And easier than as.Date(x). as.Date(x) will
> change the date to UTC for POSIXct automatically no matter whether you like
> it or not. It seems indexClass(x)<-"Date will not change the time zone of
> the date, which is exactly what I need.
> The following codes meet my needs exactly with high speed.
>> library(quantmod)
>>
>> pr<-matrix(rnorm(20000),nrow=5000,ncol=4)
>> t<-Sys.time()+(1:5000)*24*3600
>> system.time(x<-xts(pr,t))
> user system elapsed
> 0 0 0
>> head(x)
> [,1] [,2] [,3] [,4]
> 2009-07-27 09:35:39 0.6735081 -2.0724029 -2.36234114 -0.74839095
> 2009-07-28 09:35:39 -1.7756642 0.7624485 -2.15179721 -0.98905882
> 2009-07-29 09:35:39 1.2053857 -1.5182344 0.12540278 -0.02926681
> 2009-07-30 09:35:39 -0.5301832 0.3087448 -0.46227937 -1.03176589
> 2009-07-31 09:35:39 -0.9061220 1.2160150 -0.85206307 -1.13925796
> 2009-08-01 09:35:39 -0.2954440 0.7048516 0.03260057 2.04602758
>> system.time(indexClass(x)<- "Date")
> user system elapsed
> 0 0 0
>> head(x)
> [,1] [,2] [,3] [,4]
> 2009-07-27 0.6735081 -2.0724029 -2.36234114 -0.74839095
> 2009-07-28 -1.7756642 0.7624485 -2.15179721 -0.98905882
> 2009-07-29 1.2053857 -1.5182344 0.12540278 -0.02926681
> 2009-07-30 -0.5301832 0.3087448 -0.46227937 -1.03176589
> 2009-07-31 -0.9061220 1.2160150 -0.85206307 -1.13925796
> 2009-08-01 -0.2954440 0.7048516 0.03260057 2.04602758
>>
>
> Thanks Jeff and Gabor.
>
> Michael
>
>
> On Sat, Jul 25, 2009 at 10:46 PM, Jeff Ryan <jeff.a.ryan at gmail.com> wrote:
>
>> An example to clarify/confuse ;)
>>
>> > system.time(px<-xts(pr,t))
>> user system elapsed
>> 0.001 0.001 0.002
>> > system.time(dx<-xts(pr,d))
>> user system elapsed
>> 0.164 0.004 0.168
>> > indexClass(dx)
>> [1] "Date"
>> > indexClass(px)
>> [1] "POSIXt" "POSIXct"
>>
>> ## in POSIXct time as expected
>> > head(px)
>> [,1] [,2] [,3] [,4]
>> 2009-07-26 09:39:32 0.6739000 0.6124116 -0.5820407 0.1022264
>> 2009-07-27 09:39:32 -0.7806308 -0.4988508 -0.5448239 -0.5649892
>> 2009-07-28 09:39:32 1.0275375 -0.8538655 -0.6098992 1.9661175
>> 2009-07-29 09:39:32 0.6268532 -0.3144514 1.0142005 -0.2303953
>> 2009-07-30 09:39:32 -3.8062200 -0.3875341 -0.9251609 -0.4677076
>> 2009-07-31 09:39:32 -1.1192617 -0.6575085 -0.9918533 -0.8743504
>>
>> > head(dx)
>> [,1] [,2] [,3] [,4]
>> 2009-07-26 0.6739000 0.6124116 -0.5820407 0.1022264
>> 2009-07-27 -0.7806308 -0.4988508 -0.5448239 -0.5649892
>> 2009-07-28 1.0275375 -0.8538655 -0.6098992 1.9661175
>> 2009-07-29 0.6268532 -0.3144514 1.0142005 -0.2303953
>> 2009-07-30 -3.8062200 -0.3875341 -0.9251609 -0.4677076
>> 2009-07-31 -1.1192617 -0.6575085 -0.9918533 -0.8743504
>> > indexClass(px) <- "Date"
>>
>> ### simply convert the indexClass -- this is essentially costless,
>> ### and now you have an xts object 'indexed' by Date
>> > head(px)
>> [,1] [,2] [,3] [,4]
>> 2009-07-26 0.6739000 0.6124116 -0.5820407 0.1022264
>> 2009-07-27 -0.7806308 -0.4988508 -0.5448239 -0.5649892
>> 2009-07-28 1.0275375 -0.8538655 -0.6098992 1.9661175
>> 2009-07-29 0.6268532 -0.3144514 1.0142005 -0.2303953
>> 2009-07-30 -3.8062200 -0.3875341 -0.9251609 -0.4677076
>> 2009-07-31 -1.1192617 -0.6575085 -0.9918533 -0.8743504
>> > system.time(indexClass(px) <- "Date")
>> user system elapsed
>> 0.001 0.001 0.001
>>
>> Now convert back, and see if something like merge cares about the
>> index class... (hint: it doesn't)
>>
>> > system.time(indexClass(px) <- c("POSIXt","POSIXct"))
>> user system elapsed
>> 0.001 0.000 0.000
>> > indexClass(px)
>> [1] "POSIXt" "POSIXct"
>> > head(px)
>> [,1] [,2] [,3] [,4]
>> 2009-07-26 09:39:32 0.6739000 0.6124116 -0.5820407 0.1022264
>> 2009-07-27 09:39:32 -0.7806308 -0.4988508 -0.5448239 -0.5649892
>> 2009-07-28 09:39:32 1.0275375 -0.8538655 -0.6098992 1.9661175
>> 2009-07-29 09:39:32 0.6268532 -0.3144514 1.0142005 -0.2303953
>> 2009-07-30 09:39:32 -3.8062200 -0.3875341 -0.9251609 -0.4677076
>> 2009-07-31 09:39:32 -1.1192617 -0.6575085 -0.9918533 -0.8743504
>> > head(dx)
>> [,1] [,2] [,3] [,4]
>> 2009-07-26 0.6739000 0.6124116 -0.5820407 0.1022264
>> 2009-07-27 -0.7806308 -0.4988508 -0.5448239 -0.5649892
>> 2009-07-28 1.0275375 -0.8538655 -0.6098992 1.9661175
>> 2009-07-29 0.6268532 -0.3144514 1.0142005 -0.2303953
>> 2009-07-30 -3.8062200 -0.3875341 -0.9251609 -0.4677076
>> 2009-07-31 -1.1192617 -0.6575085 -0.9918533 -0.8743504
>> > system.time(merge(dx,dx))
>> user system elapsed
>> 0.001 0.000 0.002
>> > system.time(merge(px,px))
>> user system elapsed
>> 0.001 0.000 0.002
>>
>> ### Caveat: converting back and forth is a bit silly, and possibly
>> perilous, but it is
>> ### illustrative of what you can do with xts
>>
>> HTHsomewhat
>>
>> Jeff
>>
>> On Sat, Jul 25, 2009 at 7: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.
>> >
>>
>>
>>
>> --
>> Jeffrey Ryan
>> jeffrey.ryan at insightalgo.com
>>
>> ia: insight algorithmics
>> www.insightalgo.com
>>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> 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.
>
More information about the R-SIG-Finance
mailing list