首页 > 解决方案 > 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 微秒,对于一些简单+的变量来说,太多了​​。

但是,当我简化这些方法时,我想知道最佳实践是什么:

那么有什么建议呢?

谢谢

标签: rcran

解决方案


定义一个子类,ts在这种情况下两者可以共存。这应该适用于fast_tsts对象、纯向量、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)

推荐阅读