r - S4方法调度慢吗?

标签 r dispatch s4

我的 S4 类有一个被多次调用的方法。我注意到执行时间比独立调用类似函数时要慢得多。所以我在我的类中添加了一个类型为“function”的插槽,并使用该函数而不是方法。下面的示例显示了两种方法,它们都比相应的方法运行得快得多。此外,该示例表明该方法的较低速度不是由于方法必须从类中检索数据,因为即使它们也这样做,函数也更快。

当然,这种做事方式并不理想。我想知道是否有一种方法可以加速方法调度。有什么建议么?

    setClass(Class = "SpeedTest", 
      representation = representation(
        x = "numeric",
        foo1 = "function",
        foo2 = "function"
      )
    )

    speedTest <- function(n) {
      new("SpeedTest",
        x = rnorm(n),
        foo1 = function(z) sqrt(abs(z)),
        foo2 = function() {}
      )
    }

    setGeneric(
      name = "method.foo",
      def = function(object) {standardGeneric("method.foo")}
    )
    setMethod(
      f = "method.foo", 
      signature = "SpeedTest",
      definition = function(object) {
        sqrt(abs(object@x))
      }
    )

    setGeneric(
      name = "create.foo2",
      def = function(object) {standardGeneric("create.foo2")}
    )
    setMethod(
      f = "create.foo2", 
      signature = "SpeedTest",
      definition = function(object) {
        z <- object@x
        object@foo2 <- function() sqrt(abs(z))

        object
      }
    )

    > st <- speedTest(1000)
    > st <- create.foo2(st)
    > 
    > iters <- 100000
    > 
    > system.time(for (i in seq(iters)) method.foo(st)) # slowest by far
       user  system elapsed 
       3.26    0.00    3.27 

    > # much faster 
    > system.time({foo1 <- st@foo1; x <- st@x; for (i in seq(iters)) foo1(x)}) 
       user  system elapsed 
      1.47    0.00    1.46 

    > # retrieving st@x instead of x does not affect speed
    > system.time({foo1 <- st@foo1; for (i in seq(iters)) foo1(st@x)}) 
       user  system elapsed 
       1.47    0.00    1.49 

    > # same speed as foo1 although no explicit argument
    > system.time({foo2 <- st@foo2; for (i in seq(iters)) foo2()}) 
       user  system elapsed 
       1.44    0.00    1.45 

     # Cannot increase speed by using a lambda to "eliminate" the argument of method.foo
     > system.time({foo <- function() method.foo(st); for (i in seq(iters)) foo()})  
        user  system elapsed 
        3.28    0.00    3.29

最佳答案

成本在于方法查找,它在您的时间的每次迭代中从头开始。这可以通过计算一次方法调度来缩短

METHOD <- selectMethod(method.foo, class(st))
for (i in seq(iters)) METHOD(st)

这个(更好的方法查找)将是一个非常有趣和值得的项目;在其他动态语言中学到了宝贵的经验,例如,维基百科的 dynamic dispatch 中提到的内联缓存页。

我想知道您进行许多方法调用的原因是否是因为您的数据表示和方法的向量化不完整?

关于r - S4方法调度慢吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16386154/

相关文章:

javascript - React Redux - 调度检索输入值

c++ - C++ 中 func() 和 (*this).func() 的区别

r - 找到决策树中的最大值

r - 你怎么能总是抑制 R 中的消息?

r - r : Any libraries available? 中的面向方面编程

r - 如何从这些对象的列表中创建对象属性的新列表?

R:如何真正从 S4 对象中移除 S4 插槽(附有解决方案!)

r - 将数据标签添加到 ggplot : label argument not working 中的点

python - lru_cache 干扰 single_dispatch 完成的类型检查

r - 创建名为 `Median` 的 S4 方法