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
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?