润滑`period`与`as.numeric` [英] lubridate `period` versus `as.numeric`

查看:83
本文介绍了润滑`period`与`as.numeric`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么理由让我更喜欢/dseconds()而不是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

和微基准测试:

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()

推荐答案

结果基本没有太大区别.另请参阅@Stibu的答案中的评论此处.

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

这篇关于润滑`period`与`as.numeric`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆