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
This doesn't help you directly with your problem, but it's much easier to benchmark this sort of stuff with the microbenchmark package:
f <- function(x) NULL
s3 <- function(x) UseMethod("s3")
s3.integer <- function(x) NULL
A <- setClass("A", representation(a = "list"))
setGeneric("s4", function(x) standardGeneric("s4"))
setMethod(s4, "A", function(x) NULL)
B <- setRefClass("B")
B$methods(r5 = function(x) NULL)
a <- A()
b <- B$new()
library(microbenchmark)
options(digits = 3)
microbenchmark(
bare = NULL,
fun = f(),
s3 = s3(1L),
s4 = s4(a),
r5 = b$r5()
)
# Unit: nanoseconds
# expr min lq median uq max neval
# bare 13 20 22 29 36 100
# fun 171 236 270 310 805 100
# s3 2025 2478 2651 2869 8603 100
# s4 10017 11029 11528 11905 36149 100
# r5 9080 10003 10390 10804 61864 100
On my computer, the bare call takes about 20 ns. Wrapping it in a function adds about an extra 200 ns - this is the cost of creating the environment where the function execution happens. S3 method dispatch adds around 3 µs and S4/ref classes around 12 µs.