r - R 最佳实践:对于 CRAN,您应该优化还是保留基本方法
问题描述
我正在为我在 CRAN 上的包裹写信,以优化速度。
我已经看到了一个主要问题,那就是时间序列的“基本”(实际上是统计数据)方法非常慢,尤其是当您使用相同的 tsp 时。
set.seed(1)
a <- ts(rnorm(480),start=2010,freq=12)
b <- ts(rnorm(480),start=2010,freq=12)
library(microbenchmark)
ts_fastop <- function(x,y,FUN) {
FUN <- match.fun(FUN)
tspx <- tsp(x)
if (any(abs(tspx - tsp(y)) > getOption("ts.eps"))) stop("This method is only made for similar tsp", call. = FALSE)
ts(FUN(as.numeric(x),as.numeric(y)),start=tspx[1L],frequency = tspx[3L])
}
identical(ts_fastop(a,b,`+`),a+b)
# [1] TRUE
microbenchmark(ts_fastop(a,b,`+`),a+b,times=1000L)
# Unit: microseconds
# expr min lq mean median uq max neval
# ts_fastop(a, b, `+`) 13.7 15.3 24.1260 17.4 18.9 6666.4 1000
# a + b 364.5 372.5 385.7744 375.6 380.4 7218.4 1000
我认为 380 微秒,对于一些简单+
的变量来说,太多了。
但是,当我简化这些方法时,我想知道最佳实践是什么:
- 如果有人快捷方式主要功能,我想这会让 R 核心团队更不容易管理升级
- 源代码的可读性如果写成 a+b 比 ts_fastop(a,b,
+
)更好
那么有什么建议呢?
谢谢
解决方案
定义一个子类,ts
在这种情况下两者可以共存。这应该适用于fast_ts
从ts
对象、纯向量、zoo
对象xts
和其他as.ts
存在方法的对象构造对象。
as.fast_ts <- function(x, ...) UseMethod("as.fast_ts")
as.fast_ts.fast_ts <- identity
as.fact_ts.default <- function(x, ...) structure(as.ts(x, ...),
class = c("fast_ts", "ts"))
Ops.fast_ts <- function(e1, e2) {
result <- match.fun(.Generic)(c(e1), c(e2))
structure(result, tsp = tsp(e1), class = c("fast_ts", "ts"))
}
# test
set.seed(1)
a <- ts(rnorm(480),start=2010,freq=12)
b <- ts(rnorm(480),start=2010,freq=12)
af <- as.fast_ts(a)
bf <- as.fast_ts(b)
library(microbenchmark)
microbenchmark(a+b, af+bf)
推荐阅读
- javascript - 上传文件导致 Aw, Snap on chrome
- facebook - 如何将 Facebook 测试页面订阅到 Messenger 应用程序
- mysql - 以及从哪里使用 mysql join
- c++ - 如何在不重复访问时验证 C++ 对象的状态
- vb.net - YAML 文件中的嵌套列表 - 在 VB.net 中创建类时遇到问题(eve online SDE)
- markdown - 如何使用 Markdown 对同一文档的部分进行链接引用,其中标题是带有空格的句子?
- erlang - 掌握 Erlang 消息
- webrtc - 使用WebRTC,是否每次不用TURN sever都能连接成功?
- javascript - 支持小写并包含搜索值的管道
- limit - 封装的 poscript 最大行数限制