Is S4 method dispatch slow?

前端 未结 2 1072
别跟我提以往
别跟我提以往 2020-12-29 11:37

My S4 class has a method that is called many times. I noticed that the execution time is much slower than it would be if a similar function was called independently. So I ad

2条回答
  •  盖世英雄少女心
    2020-12-29 12:21

    The cost is in method look-up, which starts from scratch in each iteration of your timing. This can be short-circuited by figuring out method dispatch once

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

    This (better method look-up) would be a very interesting and worth-while project; there are valuable lessons learned in other dynamic languages, e.g., inline caching mentioned on Wikipedia's dynamic dispatch page.

    I wonder if the reason that you're making many method calls is because of incomplete vectorization of your data representation and methods?

提交回复
热议问题