I know it is possible to estimate the time it takes to complete a function (e.g., with sys.time()
, but is there a way to benchmark a function and see which element within the function takes the longest/shortest?
So for example, given the code below, I should be able to performance test the function and determine for run 1, function_long
is a bottleneck but for run 2, function_custom
is the issue.
function_short <- function(){
Sys.sleep(2)
}
function_long <- function(){
sys.sleep(8)
}
function_custom <- function(x){
sys.sleep(x)
}
function_to_test <- function(x){
print("Starting function")
print("2 second rest..")
function_short()
print("8 second rest..")
function_long()
print(paste0(x), " second rest..")
function_custom(x)
}
# test these
## run 1
function_to_test(1)
## run 2
function_to_test(10)