Expression output when calling by system.time
I have written a function which produces a list as output, and am conducting an experiment to measure its performance when run in multiple configurations. The output of the function is nested list, and is nontrivial for each configuration. Currently, I am approaching the problem using lapply and a list of rando开发者_开发问答m seed values to be provided to my function. This successfully produces the function's output for each configuration. My goal is to extend this experiment to measure the execution time of each configuration of the algorithm.
Currently, I have added an anonymous function to lapply which returns the output of my function as well as the execution time, where x is the random seed value, as follows:
function(x) {
t1<-system.time(t2 <- foo(x))[3]
return(c(t2,t1))
}
Is there a more concise way to preserve the output of the evaluated expression when using system.time? Thank you
For such experiments I would recommend e.g. the microbenchmark package among others instead of system.time()
calls, as these packages tries to deal with the challenge with minimal overhead and improved accuracy (results in nanosecond).
This would require you to write some functions, save the output of those in the global environment, and then run the experiments against your functions competing.
A small example:
foo <- function(x) assign(x, rnorm(1000), envir = .GlobalEnv)
boo <- function(x) assign(x, rbeta(1000, 0.1, 0.1), envir = .GlobalEnv)
res <- microbenchmark(foo("normal distr"), boo("beta distr"), times=100)
This example will NOT save your distinct outputs as will overwrite in all run. But you could write the inner functions to use the given seeds and append to a list in global environment if you really need it.
精彩评论