[R] Vectors of years, months, and days to dates?
Patrick Connolly
p.connolly at hortresearch.co.nz
Tue Jun 8 06:55:24 CEST 2004
On Tue, 08-Jun-2004 at 02:57PM +1200, Richard A. O'Keefe wrote:
|> Rolf Turner <rolf at math.unb.ca> attacked:
|> Have you ***any*** evidence that R's procedure degrades
|> performance, under any circumstances? (Apparently not.) In
|> that case why are you going on and on about it?
|>
|> I did the following:
|>
|> library(chron)
|> ymd.to.POSIXlt <-
|> function (y, m, d) as.POSIXlt(chron(julian(y=y, x=m, d=d)))
|> n <- 100000
|> y <- sample(1970:2004, n, replace=TRUE)
|> m <- sample(1:12, n, replace=TRUE)
|> d <- sample(1:28, n, replace=TRUE)
|> system.time(ymd.to.POSIXlt(y, m, d))
|> [1] 8.78 0.10 31.76 0.00 0.00
|> system.time(as.POSIXlt(paste(y,m,d, sep="-")))
|> [1] 14.64 0.13 53.30 0.00 0.00
|>
|> This was on a 500MHz SunBlade100, a slow machine by today's standards,
|> using R 1.9.0. There is all the evidence one could reasonably ask for
|> that going through paste() instead of providing y, m, d as numeric
|> vectors *does* degrade performance under at least some circumstances.
Ain't that just the darnest thing?
I tried on a somewhat faster machine and got this:
> system.time(ymd.to.POSIXlt(y, m, d))
[1] 2.06 0.22 2.29 0.00 0.00
> system.time(as.POSIXlt(paste(y,m,d, sep="-")))
[1] 2.82 0.04 2.86 0.00 0.00
Negligible difference, I'd have thought, but I tried a few more times
> system.time(ymd.to.POSIXlt(y, m, d))
[1] 1.12 0.11 1.23 0.00 0.00
> system.time(as.POSIXlt(paste(y,m,d, sep="-")))
[1] 2.69 0.04 2.73 0.00 0.00
> system.time(ymd.to.POSIXlt(y, m, d))
[1] 0.98 0.02 1.00 0.00 0.00
> system.time(as.POSIXlt(paste(y,m,d, sep="-")))
[1] 2.82 0.00 2.82 0.00 0.00
> system.time(ymd.to.POSIXlt(y, m, d))
[1] 0.98 0.04 1.02 0.00 0.00
> system.time(as.POSIXlt(paste(y,m,d, sep="-")))
[1] 2.65 0.03 2.68 0.00 0.00
>
ymd.to.POSIXlt benefits from repeated, but the as.POSIXlt method
doesn't. I even tried it again with a new R process and got the same
sort of thing?
What makes the difference?
--
Patrick Connolly
HortResearch
Mt Albert
Auckland
New Zealand
Ph: +64-9 815 4200 x 7188
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
I have the world`s largest collection of seashells. I keep it on all
the beaches of the world ... Perhaps you`ve seen it. ---Steven Wright
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
More information about the R-help
mailing list