Christoph
Christoph

Reputation: 7063

lubridate `period` versus `as.numeric`

Is there any reason why I should prefer /dseconds() over as.numeric? It seems the latter is a little bit faster. They both give the same results.

> as.numeric(lubridate::ymd_hms("2015-12-31 23:59:59 UTC") - lubridate::ymd_hms("2015-01-01 00:00:00 UTC"), units = "secs")
[1] 31535999
> interval(lubridate::ymd_hms("2015-01-01 00:00:00 UTC"), lubridate::ymd_hms("2015-12-31 23:59:59 UTC"))/dseconds(1)
[1] 31535999

and the microbenchmark test:

summary(microbenchmark::microbenchmark(
  as.numeric(lubridate::ymd_hms("2016-12-31 23:59:59 UTC") - lubridate::ymd_hms("2016-01-01 00:00:00 UTC"), units = "secs"),
  interval(lubridate::ymd_hms("2016-01-01 00:00:00 UTC"), lubridate::ymd_hms("2016-12-31 23:59:59 UTC"))/dseconds(1),
  times = 100L, unit = "ms"))

giving

                min       lq     mean   median       uq      max    neval
as.numeric   3.095075 3.161979 3.320435 3.225082 3.293127 5.634390   100
/dseconds(1) 3.940120 4.067465 4.209389 4.163069 4.259054 6.072688   100

I guess there is some further reason for the extra functionality interval/dseconds()

Upvotes: 1

Views: 321

Answers (1)

Christoph
Christoph

Reputation: 7063

There is basically no big difference concerning the results. See also the comment in @Stibu's answer here.

Upvotes: 1

Related Questions