有什么理由我更喜欢/dseconds()
而不是as.numeric
?似乎后者有点快。他们都给出了相同的结果。
> 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
和微基准测试:
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"))
给
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
我想还有一些额外功能interval/dseconds()